<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Views_CSS_CountDown_Inline' ) ) {

    class Pricely_Views_CSS_CountDown_Inline {

        private $ui_type;
        private $option_key;
        private $views_css_util;

        public function __construct() {

            $this->ui_type = 'countdown-inline';
            $this->option_key = 'ui_countdown_inlines';

            $this->views_css_util = Pricely_Views_CSS_Util::get_instance();
        }

        public function compose_css( $css, $options ) {

            $ui_options = $this->get_options( $options );

            foreach ( $ui_options as $ui_option ) {

                $css = $this->get_css( $css, $ui_option );
            }

            return $css;
        }

        private function get_css( $css, $ui_option ) {

            $title_selector = $this->get_selector( $ui_option, ' .pricely-timer-title' );
            $css[ $title_selector ] = $this->get_title_css_props( $ui_option );

            $timer_selector = $this->get_selector( $ui_option, ' .prly-tm-inner' );
            $css[ $timer_selector ] = $this->get_timer_css_props( $ui_option );

            $timer_content_selector = $this->get_selector( $ui_option, ' .prly-tm-content' );
            $css[ $timer_content_selector ] = $this->get_timer_content_css_props( $ui_option );

            $counter_selector = $this->get_selector( $ui_option, ' .prly-tm-value' );
            $css[ $counter_selector ] = $this->get_counter_css_props( $ui_option );

            $label_selector = $this->get_selector( $ui_option, ' .prly-tm-label' );
            $css[ $label_selector ] = $this->get_label_css_props( $ui_option );

            $container_selector = $this->get_selector( $ui_option, '', '.pricely-timer' );
            $css[ $container_selector ] = $this->get_container_css_props( $ui_option );

            return $css;
        }

        private function get_title_css_props( $ui_option ) {

            $css_props = array();

            $css_props[ 'color' ] = $ui_option[ 'title_color' ];
            $css_props[ 'background-color' ] = $ui_option[ 'title_bg_color' ];
            $css_props[ 'font-size' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'title_font_size' ], '14px' );
            $css_props[ 'font-weight' ] = $ui_option[ 'title_font_weight' ];
            $css_props[ 'font-family' ] = $ui_option[ 'title_font_family' ];
            $css_props[ 'padding' ] = $this->views_css_util->get_boundary_css_prop( $ui_option[ 'title_padding' ], '0px' );
            $css_props[ 'margin' ] = $this->views_css_util->get_boundary_css_prop( $ui_option[ 'title_margin' ], '0px' );
            $css_props[ 'border-radius' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'title_border_radius' ], '0px' );

            return $this->get_title_border_css_props( $css_props, $ui_option );
        }

        private function get_title_border_css_props( $css_props, $ui_option ) {

            $border_style = $ui_option[ 'title_border_style' ];

            if ( empty( $border_style ) || 'none' == $border_style ) {

                $css_props[ 'border-style' ] = $border_style;

                return $css_props;
            }

            $css_props[ 'border-style' ] = $border_style;
            $css_props[ 'border-color' ] = $ui_option[ 'title_border_color' ];
            $css_props[ 'border-width' ] = $this->views_css_util->get_boundary_css_prop( $ui_option[ 'title_border_width' ], '1px' );

            return $css_props;
        }

        private function get_timer_css_props( $ui_option ) {

            $css_props = array();

            $css_props[ 'background-color' ] = $ui_option[ 'timer_bg_color' ];
            $css_props[ 'padding' ] = $this->views_css_util->get_sides_size_css_prop( $ui_option[ 'timer_padding' ], '0px' );
            $css_props[ 'margin' ] = $this->views_css_util->get_boundary_css_prop( $ui_option[ 'timer_margin' ], '0px' );
            $css_props[ 'border-radius' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'timer_border_radius' ], '0px' );

            return $this->get_timer_border_css_props( $css_props, $ui_option );
        }

        private function get_timer_border_css_props( $css_props, $ui_option ) {

            $border_style = $ui_option[ 'timer_border_style' ];

            if ( empty( $border_style ) || 'none' == $border_style ) {

                $css_props[ 'border-style' ] = $border_style;

                return $css_props;
            }

            $css_props[ 'border-style' ] = $border_style;
            $css_props[ 'border-color' ] = $ui_option[ 'timer_border_color' ];
            $css_props[ 'border-width' ] = $this->views_css_util->get_boundary_css_prop( $ui_option[ 'timer_border_width' ], '1px' );

            return $css_props;
        }

        private function get_timer_content_css_props( $ui_option ) {

            $css_props = array();

            $css_props[ 'margin-right' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'timer_counters_gaps' ], '0px' );

            return $css_props;
        }

        private function get_counter_css_props( $ui_option ) {

            $css_props = array();

            $css_props[ 'color' ] = $ui_option[ 'counter_color' ];
            $css_props[ 'font-size' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'counter_font_size' ], '18px' );
            $css_props[ 'line-height' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'counter_font_size' ], '18px' );
            $css_props[ 'font-weight' ] = $ui_option[ 'counter_font_weight' ];
            $css_props[ 'font-family' ] = $ui_option[ 'counter_font_family' ];

            return $css_props;
        }

        private function get_label_css_props( $ui_option ) {

            $css_props = array();

            $css_props[ 'margin-left' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'timer_counters_labels_gaps' ], '0px' );
            $css_props[ 'color' ] = $ui_option[ 'label_color' ];
            $css_props[ 'font-size' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'label_font_size' ], '10px' );
            $css_props[ 'line-height' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'label_font_size' ], '10px' );
            $css_props[ 'font-weight' ] = $ui_option[ 'label_font_weight' ];
            $css_props[ 'font-family' ] = $ui_option[ 'label_font_family' ];
            $css_props[ 'top' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'vertical_align' ], '-2px' );

            return $css_props;
        }

        private function get_container_css_props( $ui_option ) {

            $css_props = array();

            $css_props[ 'text-align' ] = $ui_option[ 'justify_contents' ];
            $css_props[ 'width' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'width' ], '100%' );
            $css_props[ 'background-color' ] = $ui_option[ 'bg_color' ];
            $css_props[ 'padding' ] = $this->views_css_util->get_boundary_css_prop( $ui_option[ 'padding' ], '0px' );
            $css_props[ 'margin' ] = $this->views_css_util->get_boundary_css_prop( $ui_option[ 'margin' ], '0px' );
            $css_props[ 'border-radius' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'border_radius' ], '0px' );

            return $this->get_container_border_css_props( $css_props, $ui_option );
        }

        private function get_container_border_css_props( $css_props, $ui_option ) {

            $border_style = $ui_option[ 'border_style' ];

            if ( empty( $border_style ) || 'none' == $border_style ) {

                $css_props[ 'border-style' ] = $border_style;

                return $css_props;
            }

            $css_props[ 'border-style' ] = $border_style;
            $css_props[ 'border-color' ] = $ui_option[ 'border_color' ];
            $css_props[ 'border-width' ] = $this->views_css_util->get_boundary_css_prop( $ui_option[ 'border_width' ], '1px' );


            return $css_props;
        }

        private function get_selector( $ui_option, $suffix = '', $prefix = '' ) {

            return Pricely_Views_CSS::get_instance()->get_css_selector( $ui_option[ 'ui_id' ], $this->ui_type, $prefix, $suffix );
        }

        private function get_options( $options ) {

            return Pricely::get_instance()->get_options( $this->option_key, $this->get_default_options(), $options );
        }

        private function get_default_options() {

            $defaults = array();

            $defaults[] = array(
                'ui_id' => '2234343',
                'title_color' => $this->views_css_util->get_theme_prop( 'color_1' ),
                'title_bg_color' => '',
                'title_font_size' => array(
                    'size' => '14',
                    'unit' => 'px'
                ),
                'title_font_weight' => '600',
                'title_font_family' => '',
                'title_padding' => array(
                    'top' => '',
                    'right' => '',
                    'bottom' => '',
                    'left' => '',
                    'unit' => 'px'
                ),
                'title_margin' => array(
                    'top' => '',
                    'right' => '',
                    'bottom' => 10,
                    'left' => '',
                    'unit' => 'px'
                ),
                'title_border_radius' => array(
                    'size' => '0',
                    'unit' => 'px'
                ),
                'title_border_style' => 'none',
                'title_border_color' => $this->views_css_util->get_theme_prop( 'color_1' ),
                'title_border_width' => array(
                    'top' => '1',
                    'right' => '1',
                    'bottom' => '1',
                    'left' => '1',
                    'unit' => 'px'
                ),
                'timer_bg_color' => $this->views_css_util->get_theme_prop( 'color_4' ),
                'timer_padding' => array(
                    'top_bottom' => 10,
                    'left_right' => 10,
                    'unit' => 'px'
                ),
                'timer_margin' => array(
                    'top' => '',
                    'right' => '',
                    'bottom' => '',
                    'left' => '',
                    'unit' => 'px'
                ),
                'timer_counters_labels_gaps' => array(
                    'size' => '1',
                    'unit' => 'px'
                ),
                'timer_border_radius' => array(
                    'size' => '0',
                    'unit' => 'px'
                ),
                'timer_counters_gaps' => array(
                    'size' => '3',
                    'unit' => 'px'
                ),
                'timer_border_style' => 'none',
                'timer_border_color' => $this->views_css_util->get_theme_prop( 'color_1' ),
                'timer_border_width' => array(
                    'top' => '1',
                    'right' => '1',
                    'bottom' => '1',
                    'left' => '1',
                    'unit' => 'px'
                ),
                'counter_color' => $this->views_css_util->get_theme_prop( 'accent_color_3' ),
                'counter_font_size' => array(
                    'size' => '18',
                    'unit' => 'px'
                ),
                'counter_font_weight' => '',
                'counter_font_family' => '',
                'label_color' => $this->views_css_util->get_theme_prop( 'color_2' ),
                'label_font_size' => array(
                    'size' => '10',
                    'unit' => 'px'
                ),
                'label_font_weight' => '',
                'label_font_family' => '',
                'vertical_align' => array(
                    'size' => '-2',
                    'unit' => 'px'
                ),
                'justify_contents' => 'center',
                'width' => array(
                    'size' => '',
                    'unit' => 'px'
                ),
                'bg_color' => '',
                'padding' => array(
                    'top' => '',
                    'right' => '',
                    'bottom' => '',
                    'left' => '',
                    'unit' => 'px'
                ),
                'margin' => array(
                    'top' => '',
                    'right' => '',
                    'bottom' => 15,
                    'left' => '',
                    'unit' => 'px'
                ),
                'border_radius' => array(
                    'size' => '0',
                    'unit' => 'px'
                ),
                'border_style' => 'none',
                'border_color' => $this->views_css_util->get_theme_prop( 'color_1' ),
                'border_width' => array(
                    'top' => '1',
                    'right' => '1',
                    'bottom' => '1',
                    'left' => '1',
                    'unit' => 'px'
                ),
            );

            return $defaults;
        }

    }

}