<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Product_Promotion_Type_CountDown' ) ) {

    require_once 'countdown-inline.php';

    class Pricely_Product_Promotion_Type_CountDown {

        public function can_process_data( $promotion_type ) {

            return ('countdown' == $promotion_type);
        }

        public function process_data( $promotion_data, $promotion_args, $data ) {

            $processed_data = $this->get_countdown_data( $promotion_args, $data );

            if ( !$processed_data ) {

                return $promotion_data;
            }

            $promotion_data[ 'views' ] = $processed_data;

            return $promotion_data;
        }

        private function get_countdown_data( $promotion_args, $data ) {

            if ( !$this->can_process_view( $data ) ) {

                return false;
            }

            $promotion_args[ 'countdown_args' ] = $this->get_countdown_args( $promotion_args, $data );

            $views = $this->get_single_product_view( $this->get_shop_view( array(), $promotion_args ), $promotion_args );

            if ( !count( $views ) ) {

                return false;
            }

            return $views;
        }

        private function get_shop_view( $countdown_options, $promotion_args ) {

            $show_in_shop = ('no' != $promotion_args[ 'shop' ][ 'enable' ]);

            if ( !$show_in_shop ) {

                return $countdown_options;
            }

            $design_type = $this->get_design_type();

            return $design_type->get_shop_view( $countdown_options, $promotion_args );
        }

        private function get_single_product_view( $countdown_options, $promotion_args ) {

            $show_in_single_product = ('yes' == $promotion_args[ 'single_product' ][ 'enable' ]);

            if ( !$show_in_single_product ) {

                return $countdown_options;
            }

            $design_type = $this->get_design_type( $promotion_args, 'single_product' );

            return $design_type->get_single_product_view( $countdown_options, $promotion_args );
        }

        private function get_countdown_args( $promotion_args, $data ) {

            $countdown_args = $this->get_countdown_source( array( 'id' => $promotion_args[ 'id' ] ), $promotion_args, $data );

            $countdown_args[ 'auto_refresh' ] = $this->get_auto_refresh( $promotion_args, $data );

            $countdown_args[ 'auto_hide_days' ] = ('yes' == $promotion_args[ 'auto_hide_days' ]);

            return $countdown_args;
        }

        private function get_countdown_source( $countdown_args, $promotion_args, $data ) {

            $countdown_args[ 'source' ] = $promotion_args[ 'countdown_source' ];

            if ( 'custom' == $countdown_args[ 'source' ] ) {

                return $this->get_countdown_custom_source( $countdown_args, $promotion_args );
            }

            if ( 'conditions' == $countdown_args[ 'source' ] ) {

                return $this->get_countdown_conditions_source( $countdown_args, $promotion_args );
            }

            if ( 'sale_schedule' == $countdown_args[ 'source' ] ) {

                return $this->get_countdown_saleschedule_source( $countdown_args, $data );
            }

            return $countdown_args;
        }

        private function get_countdown_custom_source( $countdown_args, $promotion_args ) {

            $custom_mode = 'date';

            if ( !empty( $promotion_args[ 'custom_mode' ] ) ) {

                $custom_mode = $promotion_args[ 'custom_mode' ];
            }

            $countdown_args[ 'custom_mode' ] = $custom_mode;

            if ( !empty( $promotion_args[ 'date' ] ) ) {

                $countdown_args[ 'date' ] = $promotion_args[ 'date' ];
            }

            if ( !empty( $promotion_args[ 'from_date' ] ) ) {

                $countdown_args[ 'from_date' ] = $promotion_args[ 'from_date' ];
            }

            if ( !empty( $promotion_args[ 'to_date' ] ) ) {

                $countdown_args[ 'to_date' ] = $promotion_args[ 'to_date' ];
            }

            if ( !empty( $promotion_args[ 'from_time' ] ) ) {

                $countdown_args[ 'from_time' ] = $promotion_args[ 'from_time' ];
            }

            if ( !empty( $promotion_args[ 'to_time' ] ) ) {

                $countdown_args[ 'to_time' ] = $promotion_args[ 'to_time' ];
            }

            if ( !empty( $promotion_args[ 'from_datetime' ] ) ) {

                $countdown_args[ 'from_datetime' ] = $promotion_args[ 'from_datetime' ];
            }

            if ( !empty( $promotion_args[ 'to_datetime' ] ) ) {

                $countdown_args[ 'to_datetime' ] = $promotion_args[ 'to_datetime' ];
            }

            if ( 'daysofweek' == $custom_mode && isset( $promotion_args[ 'daysofweek' ] ) ) {

                $countdown_args[ 'daysofweek' ] = $promotion_args[ 'daysofweek' ];
            }

            if ( 'daysofmonth' == $custom_mode && isset( $promotion_args[ 'daysofmonth' ] ) ) {

                $countdown_args[ 'daysofmonth' ] = $promotion_args[ 'daysofmonth' ];
            }

            if ( 'monthsofyear' == $custom_mode && isset( $promotion_args[ 'monthsofyear' ] ) ) {

                $countdown_args[ 'monthsofyear' ] = $promotion_args[ 'monthsofyear' ];
            }

            return $countdown_args;
        }

        private function get_countdown_conditions_source( $countdown_args, $promotion_args ) {

            $countdown_args[ 'setting_id' ] = $promotion_args[ 'setting_id' ];

            $countdown_args[ 'module' ] = $promotion_args[ 'module' ];

            return $countdown_args;
        }

        private function get_countdown_saleschedule_source( $countdown_args, $data ) {

            $countdown_args[ 'product_id' ] = $data[ 'wc' ][ 'product' ][ 'product_id' ];

            return $countdown_args;
        }

        private function get_auto_refresh( $promotion_args, $data ) {

            $auto_refresh = array(
                'shop' => array(
                    'enable' => ('no' != $promotion_args[ 'refresh_shop' ]),
                    'url' => '',
                ),
                'single' => array(
                    'enable' => ('no' != $promotion_args[ 'refresh_single' ]),
                    'url' => '',
                ),
            );

            if ( 'redirect_page' == $promotion_args[ 'refresh_single' ] && !empty( $promotion_args[ 'page_id' ] ) ) {

                $auto_refresh[ 'single' ][ 'url' ] = get_permalink( $promotion_args[ 'page_id' ] );
            }

            if ( 'redirect_post' == $promotion_args[ 'refresh_single' ] && !empty( $promotion_args[ 'post_id' ] ) ) {

                $auto_refresh[ 'single' ][ 'url' ] = get_permalink( $promotion_args[ 'post_id' ] );
            }

            if ( 'redirect_product' == $promotion_args[ 'refresh_single' ] && !empty( $promotion_args[ 'product_id' ] ) ) {

                $auto_refresh[ 'single' ][ 'url' ] = get_permalink( $promotion_args[ 'product_id' ] );
            }

            if ( 'redirect_product' == $promotion_args[ 'refresh_single' ] && !empty( $promotion_args[ 'url' ] ) ) {

                $auto_refresh[ 'single' ][ 'url' ] = $promotion_args[ 'url' ];
            }

            if ( 'refresh' == $promotion_args[ 'refresh_single' ] ) {

                $auto_refresh[ 'single' ][ 'url' ] = get_permalink( $data[ 'wc' ][ 'product' ][ 'product_id' ] );
            }

            return $auto_refresh;
        }

        private function get_design_type() {

            return Pricely_Product_Promotion_CountDown_Inline::get_instance();
        }

        private function can_process_view( $data ) {

            if ( !isset( $data[ 'wc' ][ 'product' ] ) ) {

                return false;
            }

            $product_data = $data[ 'wc' ][ 'product' ];

            if ( 'variable' == $product_data[ 'type' ] && $product_data[ 'variation_id' ] > 0 ) {

                return false;
            }

            return true;
        }

    }

}

