<?php

if ( !defined( 'ABSPATH' ) ) {

    exit;
}

if ( !class_exists( 'Pricely_Condition_Type_Calendar' ) ) {

    class Pricely_Condition_Type_Calendar {

        private $validation_util;
        private $datetime_util;
        private $view_countdown_data;

        public function can_validate( $condition_type, $args ) {

            $condition_types = array(
                'date',
                'time'
            );

            return in_array( $condition_type, $condition_types );
        }

        public function validate( $condition, $data, $items ) {

            if ( is_null( $this->validation_util ) ) {

                $this->validation_util = Pricely_Validation_Util::get_instance();
            }

            if ( is_null( $this->datetime_util ) ) {

                $this->datetime_util = Pricely_DateTime_Util::get_instance();
            }

            if ( is_null( $this->view_countdown_data ) ) {

                $this->view_countdown_data = Pricely_View_Data_CountDown::get_instance();
            }

            $condition_type = $condition[ 'condition_type' ];

            if ( 'date' == $condition_type ) {

                return $this->validate_date( $condition );
            }

            if ( 'time' == $condition_type ) {

                return $this->validate_time( $condition );
            }

            if ( 'date_time' == $condition_type ) {

                return $this->validate_date_time( $condition );
            }

            return true;
        }

        private function validate_date( $condition ) {

            if ( !isset( $condition[ 'date_type' ] ) ) {

                return false;
            }

            $date_type = $condition[ 'date_type' ];

            if ( $date_type == 'date' ) {

                $rule_date = $condition[ 'date' ];

                if ( $rule_date == '' ) {

                    return false;
                }

                $current_date_time = $this->datetime_util->get_current_date( true );


                $from_is_valid = $this->validation_util->validate_date( '>=', $current_date_time, $rule_date . ' 00:00:00', 'Y-m-d H:i:s', 'Y-m-d H:i:s' );
                $to_is_valid = $this->validation_util->validate_date( '<=', $current_date_time, $rule_date . ' 23:59:59', 'Y-m-d H:i:s', 'Y-m-d H:i:s' );

                return ($from_is_valid == true && $to_is_valid == true);
            } else {
                $rule_date = $condition[ 'date' ];

                if ( $rule_date == '' ) {

                    return false;
                }

                $current_date_time = $this->datetime_util->get_current_date( true );

                $valid_type = '>=';
                $date_time = ' 00:00:00';

                if ( $date_type == 'to' ) {
                    $valid_type = '<=';
                    $date_time = ' 23:59:59';
                }

                return $this->validation_util->validate_date( $valid_type, $current_date_time, $rule_date . $date_time, 'Y-m-d H:i:s', 'Y-m-d H:i:s' );
            }
        }

        private function validate_time( $condition ) {

            if ( !isset( $condition[ 'time_type' ] ) ) {

                return false;
            }

            $rule_time_type = $condition[ 'time_type' ];

            if ( $rule_time_type == 'between' ) {

                $rule_from_time = $condition[ 'from_time' ];
                $rule_to_time = $condition[ 'to_time' ];

                if ( $rule_from_time == '' || $rule_to_time == '' ) {

                    return false;
                }

                $today = $this->datetime_util->get_current_date( false ) . ' ';
                $current_date_time = $this->datetime_util->get_current_date( true );


                $from_is_valid = $this->validation_util->validate_date( '>=', $current_date_time, $today . $rule_from_time, 'Y-m-d H:i:s', 'Y-m-d H:i:s' );
                $to_is_valid = $this->validation_util->validate_date( '<=', $current_date_time, $today . $rule_to_time, 'Y-m-d H:i:s', 'Y-m-d H:i:s' );

                return ($from_is_valid == true && $to_is_valid == true);
            } else {

                $rule_time = $condition[ 'time' ];

                if ( $rule_time == '' ) {

                    return false;
                }

                $today = $this->datetime_util->get_current_date( false ) . ' ';
                $current_date_time = $this->datetime_util->get_current_date( true );

                $valid_type = '>=';
                if ( $rule_time_type == 'to' ) {
                    $valid_type = '<=';
                }

                return $this->validation_util->validate_date( $valid_type, $current_date_time, $today . $rule_time, 'Y-m-d H:i:s', 'Y-m-d H:i:s' );
            }
        }

    }

}