<?php

namespace AdminStarterPack;

if (!defined('WPINC')) { die; }

class AspPluginCore {

    public function __construct()
    {
        add_action('admin_menu', array($this, 'add_option_menu'));
        
		add_filter('plugin_action_links_' . STARTERPACK_BASENAME, [$this, 'plugin_setting_links']);
		add_filter('plugin_row_meta', array($this, 'plugin_row_links'), 10, 2);
    }

    /**
     * Add starterpack to setting menu
     *
     * @return void
     */
     
    /* add settings on admin sidebar */
    public function add_option_menu()
    {
		
        //$page = add_options_page(
		$page = add_menu_page(
            STARTERPACK_NAME,		// Page title
            STARTERPACK_NAME,		// Menu name
            'manage_options', 					// Permissions
            STARTERPACK_SHORT_SLUG,					// Menu slug
            array($this, 'view'),                // Function callback
			'dashicons-layout',
			1
        );

        add_action('load-' . $page, array($this, 'load'));

    }
    
	
	/* add settings on plugin list */
	public function plugin_setting_links($links)
    {
        $slug = STARTERPACK_SHORT_SLUG;
        $links = array_merge(array(
            '<a href="' . esc_url(admin_url('/admin.php?page="'.$slug.'"')) . '">' . __('Settings', 'starterpack') . '</a>',
        ), $links);
        
        return $links;
    }
    
    /* add links on plugin list row */
    public function plugin_row_links($links, $file)
      {
        if ($file !== STARTERPACK_BASENAME ) {
          return $links;
        }
    
        $pro_link = '<a target="_blank" href="https://dhiratara.me/services/speed-optimization/" title="' . __('Optimize More', 'starterpack') . '">' . __('Speed Up Your Site!', 'starterpack') . '</a>';
    
        $links[] = $pro_link;
    
        return $links;
      } // plugin_meta_links

    /**
     * starterpack setting menu page is loaded
     *
     * @return void
     */
    public function load()
    {

        do_action("starterpack_load_page");

        // Register scripts
        add_action("admin_enqueue_scripts", array($this, 'enqueue_scripts'));

        //check store;
        $this->store();
    }

    public function enqueue_scripts()
    {

        $setting_js = 'js/admin-settings.js';
        wp_register_script(
            'starterpack-admin-settings',
            STARTERPACK_ASSETS_URL . $setting_js, '',
            array('jquery', 'select2', 'wp-color-picker', 'jquery-validate'), 
            STARTERPACK_VERSION
        );
        /*
        wp_register_script(
            'twsp-admin-settings',
            TWSP_ASSETS_URL . $setting_js,
            array('jquery', 'select2', 'wp-color-picker', 'jquery-validate'),
            TWSP_VERSION . '2'
        );
        */
        $jquery_validate = 'js/jquery.validate.min.js';
        wp_register_script(
            'jquery-validate',
            STARTERPACK_ASSETS_URL . $jquery_validate,
            array('jquery'),
            STARTERPACK_VERSION
        );
		
        $ays_before_js = 'js/ays-beforeunload-shim.js';
        wp_register_script(
            'ays-beforeunload-shim',
            STARTERPACK_ASSETS_URL . $ays_before_js,
            array('jquery'),
            STARTERPACK_VERSION
        );

        $areyousure_js = 'js/jquery-areyousure.js';
        wp_register_script(
            'jquery-areyousure',
            STARTERPACK_ASSETS_URL . $areyousure_js,
            array('jquery'),
            STARTERPACK_VERSION
        );

        $setting_css = 'css/admin-settings.css';
        wp_register_style(
            'starterpack-admin-settings',
            STARTERPACK_ASSETS_URL . $setting_css, '',
            STARTERPACK_VERSION
        );
        
        wp_register_style('select2', STARTERPACK_ASSETS_URL . 'css/select2.min.css');
        wp_register_script('select2', STARTERPACK_ASSETS_URL . 'js/select2.min.js');
        
        wp_enqueue_script('wp-color-picker-alpha', STARTERPACK_ASSETS_URL . 'js/wp-color-picker-alpha.min.js', array( 'wp-color-picker' ));
		wp_enqueue_script('wp-color-picker-init', STARTERPACK_ASSETS_URL . 'js/wp-color-picker-init.js', array( 'wp-color-picker-alpha' ));

        //wp_enqueue_script(array('ays-beforeunload-shim', 'jquery-areyousure', 'wp-color-picker-alpha', 'starterpack-admin-settings'));
        //wp_enqueue_style(array('starterpack-admin-settings'));
        wp_enqueue_script(array('select2', 'wp-color-picker', 'ays-beforeunload-shim', 'jquery-areyousure', 'wp-color-picker-alpha', 'starterpack-admin-settings'));
        wp_enqueue_style(array('select2', 'wp-color-picker', 'starterpack-admin-settings'));
		
        wp_localize_script(
            'starterpack-admin-settings',
            'starterpack_settings',
            array(
                'loginurl' => site_url("/wp-login.php"),
                'adminurl' => admin_url("index.php"),
                'starterpack_ajax_nonce' => wp_create_nonce("starterpack_ajax_nonce")
            )
        );
    }

    private function store()
    {
        do_action('starterpack_save_before_validation');

        if (!isset($_POST['starterpack-settings'])) {
            return;
        }

        if (isset($_POST['starterpack-action']) && $_POST['starterpack-action'] == 'reset') {
            return;
        }
        //  nonce checking
        if (!isset($_POST['starterpack_settings_nonce'])
            || !wp_verify_nonce($_POST['starterpack_settings_nonce'], 'starterpack-settings-action')) {

            print 'Sorry, your nonce did not verify.';
            exit;
        }

        starterpack_instance()->Settings()->store();
        return;
    }

    public function view()
    {
        $starterpack = starterpack_instance();
        $view = $starterpack->get_active_view();
        $starterpack->admin_view($view);
    }
    
    
}