<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Views_CSS_Price_Table' ) ) {


    class Pricely_Views_CSS_Price_Table {

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

        public function __construct() {

            $this->ui_type = 'price-table';
            $this->option_key = 'ui_pricing_tables';

            $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 ) {

            $table_selector = $this->get_selector( $ui_option, '' );
            $css[ $table_selector ] = $this->get_table_css_props( $ui_option );


            $table_cell_selector_th = $this->get_selector( $ui_option, ' table th' );
            $table_cell_selector_td = $this->get_selector( $ui_option, ' table td' );
            $css[ $table_cell_selector_th . ',' . $table_cell_selector_td ] = $this->get_cells_css_props( $ui_option );

            $table_cell_header_selector = $this->get_selector( $ui_option, ' table th' );
            $css[ $table_cell_header_selector ] = $this->get_cells_header_css_props( $ui_option );

            $table_cell_data_selector = $this->get_selector( $ui_option, ' table td' );
            $css[ $table_cell_data_selector ] = $this->get_cells_data_css_props( $ui_option );

            $table_cell_suffix_selector = $this->get_selector( $ui_option, ' table td .prly-pr-tb-suffix' );
            $css[ $table_cell_suffix_selector ] = $this->get_suffix_css_props( $ui_option );

            return $css;
        }

        private function get_table_css_props( $ui_option ) {

            $css_props = array();

            $css_props[ 'width' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'width' ], 'auto' );
            $css_props[ 'margin' ] = $this->views_css_util->get_boundary_css_prop( $ui_option[ 'margin' ], '0px' );

            return $css_props;
        }

        private function get_cells_css_props( $ui_option ) {

            $css_props = array();

            if ( empty( $ui_option[ 'border_style' ] ) || 'none' == $ui_option[ 'border_style' ] ) {

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

                return $css_props;
            }

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

            $css_props[ 'padding' ] = $this->views_css_util->get_sides_size_css_prop( $ui_option[ 'padding' ], '0px' );
            $css_props[ 'font-size' ] = $this->views_css_util->get_size_css_prop( $ui_option[ 'font_size' ], '12px' );


            return $css_props;
        }

        private function get_cells_header_css_props( $ui_option ) {

            $css_props = array();

            $css_props[ 'color' ] = $ui_option[ 'header_text_color' ];
            $css_props[ 'background-color' ] = $ui_option[ 'header_bg_color' ];

            return $css_props;
        }

        private function get_cells_data_css_props( $ui_option ) {

            $css_props = array();

            $css_props[ 'color' ] = $ui_option[ 'data_text_color' ];
            $css_props[ 'background-color' ] = $ui_option[ 'data_bg_color' ];

            return $css_props;
        }

        private function get_suffix_css_props( $ui_option ) {

            $css_props = array();

            $css_props[ 'color' ] = $ui_option[ 'data_suffix_color' ];

            return $css_props;
        }

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

            return Pricely_Views_CSS::get_instance()->get_css_selector( $ui_option[ 'ui_id' ], $this->ui_type, '', $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',
                'width' => array(
                    'size' => '',
                    'unit' => 'px'
                ),
                'margin' => array(
                    'top' => '0',
                    'right' => '-1',
                    'bottom' => '0',
                    'left' => '-1',
                    'unit' => 'px'
                ),
                'font_size' => array(
                    'size' => '12',
                    'unit' => 'px'
                ),
                'border_style' => 'solid',
                'border_color' => $this->views_css_util->get_theme_prop( 'color_5' ),
                'border_width' => array(
                    'size' => '1',
                    'unit' => 'px'
                ),
                'padding' => array(
                    'top_bottom' => '5',
                    'left_right' => '8',
                    'unit' => 'px'
                ),
                'header_text_color' => $this->views_css_util->get_theme_prop( 'color_1' ),
                'header_bg_color' => $this->views_css_util->get_theme_prop( 'color_3' ),
                'data_text_color' => $this->views_css_util->get_theme_prop( 'color_1' ),
                'data_bg_color' => $this->views_css_util->get_theme_prop( 'color_4' ),
                'data_suffix_color' => $this->views_css_util->get_theme_prop( 'color_2' ),
            );

            return $defaults;
        }

    }

}

