<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

    class Eber_Menu {
        
        public static function add_main_menu(){
            add_menu_page(
                __( 'Eber Configuration', 'https://eber.co' ),
                'Eber Connect',
                'manage_options',
                'eber-config',
                array('Eber_Menu','SettingsPage'),
                'dashicons-admin-links',
                81
            );
        
        }
        public function SettingsPage(){
            $success = false;
            $api_key = get_option('eber_api_key','');
            $status = get_option('eber_status',false);
            $sync_non_member = get_option('eber_sync_non_member',false);
            $eber_custom_link = get_option('eber_custom_link',false);
            $eber_custom_login_url = get_option('eber_custom_login_url','');
            $eber_custom_signup_url = get_option('eber_custom_signup_url','');
            $adjust_point_when = get_option('adjust_point_when','order_completed');
            $eber_welcome_email = get_option('eber_welcome_email',false);
            $exclude_tax = get_option('exclude_tax',true);
            $exclude_shipping = get_option('exclude_shipping',true);
            $exclude_coupon = get_option('exclude_coupon',true);
            $widget_new = get_option('widget_new',false);
            $sync = get_option('eber_sync',false);
            if($api_key != ''){
                $api_key = get_option('eber_store_id','').'_'.$api_key;
            }
            $old_api_key = $api_key;
            if(isset($_POST['_wp_http_referer'])){
                $status = (isset($_POST['status']));
                $sync = (isset($_POST['eber_sync']));
                $sync_non_member =  (isset($_POST['eber_sync_non_member']));
                $eber_custom_link = (isset($_POST['eber_custom_link']));
                $eber_welcome_email = (isset($_POST['eber_welcome_email']));

                $exclude_tax = (isset($_POST['exclude_tax']));
                $exclude_shipping = (isset($_POST['exclude_shipping']));
                $exclude_coupon = (isset($_POST['exclude_coupon']));
                $widget_new = (isset($_POST['widget_new']));
                if(isset($_POST['eber_custom_login_url']))
                {
                    $eber_custom_login_url = $_POST['eber_custom_login_url'];
                }
                if(isset($_POST['eber_custom_signup_url']))
                {
                    $eber_custom_signup_url = $_POST['eber_custom_signup_url'];
                }
                $error = array();
                if($_POST['api_key'] == ''){
                    $error[] = 'API Key field should be required';
                }

                if(!current_user_can( 'manage_options' )){
                    $error[] = 'Permission Denied';
                }

                if(!wp_verify_nonce(sanitize_text_field($_POST['_wpnonce']),'has_'.get_option('eber_hash_key'))){
                    $error[] = 'Security Check fail';
                }

                if(count($error) == 0){
                    $sanitize_api_key = sanitize_text_field($_POST['api_key']);
                    $api_key = explode('_',$sanitize_api_key);
                    if(count($api_key) == 1){
                        $error[] = 'API KEY Not valid';
                        $api_key = $sanitize_api_key;
                    }
                    else {
                        
                       
                            $call_api = Eber_Menu::call_api(sanitize_text_field($api_key[0]),sanitize_text_field($api_key[1]));
                            if($call_api['success']){
                                update_option('eber_api_key',sanitize_text_field($api_key[1]));
                                update_option('eber_store_id',sanitize_text_field($api_key[0]));
                                $api_key = sanitize_text_field($api_key[0]).'_'.sanitize_text_field($api_key[1]);
                                if(isset($_POST['status'])){
                                    update_option('eber_status',sanitize_text_field($_POST['status']));
                                }
                                else {
                                    update_option('eber_status',false);
                                }
                                if(isset($_POST['widget_new']))
                                {
                                    update_option('widget_new',sanitize_text_field($_POST['widget_new']));
                                }
                                else
                                {
                                    update_option('widget_new',false);
                                }
                                if(isset($_POST['eber_sync'])){
                                    update_option('eber_sync',sanitize_text_field($_POST['eber_sync']));
                                }
                                else {
                                    update_option('eber_sync',false);
                                }
                                if(isset($_POST['eber_sync_non_member']))
                                {
                                    update_option('eber_sync_non_member',sanitize_text_field($_POST['eber_sync_non_member']));
                                }
                                else
                                {
                                    update_option('eber_sync_non_member',false);
                                }
                                if(isset($_POST['eber_custom_link']))
                                {
                                    update_option('eber_custom_link',sanitize_text_field($_POST['eber_custom_link']));
                                }
                                else
                                {
                                    update_option('eber_custom_link',false);
                                }
                                if(isset($_POST['eber_custom_login_url']))
                                {
                                    update_option('eber_custom_login_url',sanitize_text_field($_POST['eber_custom_login_url']));
                                }
                                else
                                {
                                    update_option('eber_custom_login_url','');
                                }
                                if(isset($_POST['eber_custom_signup_url']))
                                {
                                    update_option('eber_custom_signup_url',sanitize_text_field($_POST['eber_custom_signup_url']));
                                }
                                else
                                {
                                    update_option('eber_custom_signup_url','');
                                }
                                if(isset($_POST['adjust_point_when']))
                                {
                                    update_option('adjust_point_when',sanitize_text_field($_POST['adjust_point_when']));
                                    $adjust_point_when = $_POST['adjust_point_when'];
                                }
                                if(isset($_POST['eber_welcome_email']))
                                {
                                    update_option('eber_welcome_email',true);
                                }
                                else 
                                {
                                    update_option('eber_welcome_email',false);
                                }


                                if(isset($_POST['exclude_tax']))
                                {
                                    update_option('exclude_tax',true);
                                }
                                else
                                {
                                    update_option('exclude_tax',false);
                                }

                                if(isset($_POST['exclude_coupon']))
                                {
                                    update_option('exclude_coupon',true);
                                }
                                else
                                {
                                    update_option('exclude_coupon',false);
                                }

                                if(isset($_POST['exclude_shipping']))
                                {
                                    update_option('exclude_shipping',true);
                                }
                                else
                                {
                                    update_option('exclude_shipping',false);
                                }


                                $success = true;
                            }
                            else {
                                $error[] = $call_api['message'];
                                $api_key = sanitize_text_field($api_key[0]).'_'.sanitize_text_field($api_key[1]);
                            }
                        
                        
                        
                    }
                    
                }
                
            }
           
            require_once(dirname(__FILE__).'/../layout/index.php');
        }
        
        public static function call_api($store_id,$api_key){
            require_once(dirname(__FILE__).'/api/API.php');
            require_once(dirname(__FILE__).'/api/APIResponse.php');
            $api = new API();
            $result = $api->setData(['provider' => 'wordpress', 'provider_identifier' => get_option('eber_hash_key',''),'store_id'=>$store_id])
                    ->setQueue()
                    ->requireAuth($api_key)
                    ->post('/v3/ecom/integration/wordpress/connect');
                    
            if(isset($result->http_code) && $result->http_code == 200){
                $business = $result->data['business'];
                update_option('eber_business_name',$business['name']);
                update_option('eber_business_slug',$business['slug']);
                update_option('eber_business_id',$business['id']);
                return array(
                    'success'=>true,
                    'message'=>''
                    );
                
            }
            else {
                
                return array(
                    'success'=>false,
                    'message'=>$result->data['error']
                    );
            }
        }
    }