<?php

class SyrusSettings {
    private const OLD_OPTIONS_NAME = [
        'syrus_theme_layout',
        'syrus_theme_notice_dismiss',
        'syrus_theme_social_twitter_username',
        'syrus_theme_generali_chatgpt_token',
        'syrus_theme_generali_deepl_api_key',
        'syrus_theme_generali_http_cleaner',
        'syrus_theme_generali_p_cleaner',
        'syrus_theme_generali_span_replace',
        'syrus_theme_generali_infinite_scroll_home',
        'syrus_theme_generali_infinite_scroll',
        'syrus_theme_generali_controlli_seo',
        'syrus_theme_file_ads_txt',
        'syrus_theme_file_robots_txt',
        'syrus_theme_file_abilita_ads_txt',
        'syrus_theme_file_abilita_robots_txt',
        'syrus_theme_ads_head',
        'syrus_theme_ads_footer',
        'syrus_theme_ads_before',
        'syrus_theme_ads_after',
        'syrus_theme_ads_blocco_1',
        'syrus_theme_ads_blocco_2',
        'syrus_theme_ads_blocco_3',
    ];

    private const OPTION_NAME = "syrus_theme_settings";

    private $layout = 1;
    private $notice_plugin_dismiss = false;
    private $chatGPT_token = null;
    private $deepl_api_key = null;

    private $post_content_filters = [
        'http-to-https' => false,
        'p-cleaner' => false,
        'span-remover' => false
    ];

    private $infinite_scroll = [
        'single' => false,
        'home' => false,
    ];

    private $seo_controls = [
        'tag-h1-not-allowed' => false,
        'tag-h2-required' => false,
        'tag-h3-required' => false,
        'h2-unlike-title' => false,
        'h3-unlike-h2' => false,
        'min-500-words' => false,
        'featured-image-required' => false
    ];

    private $ads_txt = [
        'allow_virtualization' => false,
        'content' => null,
    ];

    private $robots_txt = [
        'allow_virtualization' => false,
        'content' => null,
    ];

    private $content_injection = [
        'head' => null,
        'before' => null,
        'blocco_1' => null,
        'blocco_2' => null,
        'blocco_3' => null,
        'after' => null,
        'footer' => null,
    ];

    public function get_layout() {
        return $this->layout;
    }

    public function get_notice_plugin_dismiss() {
        return $this->notice_plugin_dismiss;
    }

    public function get_chatGPT_token() {
        return $this->chatGPT_token;
    }

    public function get_deepl_api_key() {
        return $this->deepl_api_key;
    }

    public function get_post_content_filters() {
        return $this->post_content_filters;
    }

    public function get_infinite_scroll() {
        return $this->infinite_scroll;
    }

    public function get_seo_controls() {
        return $this->seo_controls;
    }

    public function get_ads_txt() {
        return $this->ads_txt;
    }

    public function get_robots_txt() {
        return $this->robots_txt;
    }

    public function get_content_injection() {
        return $this->content_injection;
    }

    public function initalize() {
        $current_settings = get_option(self::OPTION_NAME, null);

        if(!$current_settings)
            return $this->restore_old_settings();
    
        return $this->hydrate($current_settings);
    }

    private function restore_old_settings() {
        foreach(self::OLD_OPTIONS_NAME as $option_name) {
            $option_value = get_option($option_name, null);

            switch($option_name) {
                case 'syrus_theme_layout':
                    $this->layout = $option_value ?: null;
                    break;
                case 'syrus_theme_notice_dismiss':
                    $this->notice_plugin_dismiss = $option_value ? true : false;
                    break;
                case 'syrus_theme_generali_chatgpt_token':
                    $this->chatGPT_token = $option_value ?: null;
                    break;
                case 'syrus_theme_generali_deepl_api_key':
                    $this->deepl_api_key = $option_value ?: null;
                    break;
                case 'syrus_theme_generali_http_cleaner':
                    $this->post_content_filters['http-to-https'] = $option_value ? true : false;
                    break;
                case 'syrus_theme_generali_p_cleaner':
                    $this->post_content_filters['p-cleaner'] = $option_value ? true : false;
                    break;
                case 'syrus_theme_generali_span_replace':
                    $this->post_content_filters['span-remover'] = $option_value ? true : false;
                    break;
                case 'syrus_theme_generali_infinite_scroll_home':
                    $this->infinite_scroll['home'] = $option_value ? true : false;
                    break;
                case 'syrus_theme_generali_infinite_scroll':
                    $this->infinite_scroll['single'] = $option_value ? true : false;
                    break;
                case 'syrus_theme_generali_controlli_seo':
                    $single_controls = explode(',', $option_value);
                    $this->seo_controls['tag-h1-not-allowed'] = in_array('tag-h1-presente',$single_controls);
                    $this->seo_controls['tag-h2-required'] = in_array('tag-h2-non-presente',$single_controls);
                    $this->seo_controls['tag-h3-required'] = in_array('tag-h3-non-presente',$single_controls);
                    $this->seo_controls['tag-h3-required'] = in_array('tag-h3-non-presente',$single_controls);
                    $this->seo_controls['h2-unlike-title'] = in_array('tag-h2-uguale-al-titolo',$single_controls);
                    $this->seo_controls['h3-unlike-h2'] = in_array('tag-h2-e-h3-duplicato',$single_controls);
                    $this->seo_controls['min-500-words'] = in_array('min-500-parole',$single_controls);
                    $this->seo_controls['featured-image-required'] = in_array('immagine-in-evidenza-non-presente',$single_controls);
                    break;
                case 'syrus_theme_file_abilita_ads_txt':
                    $this->ads_txt['allow_virtualization'] = $option_value ? true : false;
                    break;
                case 'syrus_theme_file_abilita_robots_txt':
                    $this->robots_txt['allow_virtualization'] = $option_value ? true : false;
                    break;
                case 'syrus_theme_file_ads_txt':
                    $this->ads_txt['content'] = $option_value ?: null;
                    break;
                case 'syrus_theme_file_robots_txt':
                    $this->robots_txt['content'] = $option_value ?: null;
                    break;
                case 'syrus_theme_ads_head':
                    $this->content_injection['head'] = $option_value ?: null;
                    break;
                case 'syrus_theme_ads_footer':
                    $this->content_injection['footer'] = $option_value ?: null;
                    break;
                case 'syrus_theme_ads_before':
                    $this->content_injection['before'] = $option_value ?: null;
                    break;
                case 'syrus_theme_ads_after':
                    $this->content_injection['after'] = $option_value ?: null;
                    break;
                case 'syrus_theme_ads_blocco_1':
                    $this->content_injection['blocco_1'] = $option_value ?: null;
                    break;
                case 'syrus_theme_ads_blocco_2':
                    $this->content_injection['blocco_2'] = $option_value ?: null;
                    break;
                case 'syrus_theme_ads_blocco_3':
                    $this->content_injection['blocco_3'] = $option_value ?: null;
                    break;
            }
        }

        return $this->save();
    }

    private function save() {
        $arr = [];

        $reflect = new ReflectionClass($this);
        $props = $reflect->getProperties();

        foreach($props as $key) {
            $prop = $key->name;
            $arr[$prop] = $this->$prop;
        }

        $serialized_data = serialize($arr);
        update_option(self::OPTION_NAME, $serialized_data);
        return $this->hydrate($serialized_data);
    }

    private function hydrate($serialized_data) {
        foreach(unserialize($serialized_data) as $key => $val) {
            $this->$key = $val;
        }
        return $this;
    }
    


}