<?php 
defined('ABSPATH') || die('Cheatin\' uh?');

class STRB_Controllers_OnboardingSettings extends STRB_Classes_FrontController {

    public function init(){
        $tabs = preg_replace("/[^a-zA-Z0-9]/", "", STRB_Classes_Helpers_Tools::getValue('tab','initial'));
    
    if ($tabs && method_exists($this, $tabs)) {
        call_user_func(array($this, $tabs));
         }
    }

    public function activate(){
        $tab = preg_replace("/[^a-zA-Z0-9]/", "", STRB_Classes_Helpers_Tools::getValue('tab','activation'));
        $this->show_view('Settings/' . esc_attr(ucfirst($tab)));
    }
   
    public function initialSetupData(){
        $tab = preg_replace("/[^a-zA-Z0-9]/", "", STRB_Classes_Helpers_Tools::getValue('tab','initialSetup'));
        $this->show_view('Settings/' . esc_attr(ucfirst($tab)));
    }

    public function style(){
        $tab = preg_replace("/[^a-zA-Z0-9]/", "", STRB_Classes_Helpers_Tools::getValue('tab','style'));
        $this->show_view('Settings/' . esc_attr(ucfirst($tab)));
    }

    public function action() {
        parent::action();
        switch (STRB_Classes_Helpers_Tools::getValue('action')) {

            case 'strb_settings_onboarding':
                if (!STRB_Classes_Helpers_Tools::userCan('edit_users')) {
                    return;}
             if(STRB_Classes_Helpers_Tools::getValue('user-selected')==1){
               $this->saveUserValues();
               $this->saveDummyUserValues();}
              // $this->saveDefaultUserValues();
                STRB_Classes_Helpers_Tools::emptyCache();
            break;

            case 'strb_settings_activate':
                if (!STRB_Classes_Helpers_Tools::userCan('edit_users')) {
                    return;}
                //$this->activationKey();      
                STRB_Classes_Helpers_Tools::emptyCache();
            break;

            case 'strb_settings_prev':
                if (!STRB_Classes_Helpers_Tools::userCan('edit_users')) {
                    return;}         
                wp_redirect(STRB_Classes_Helpers_Tools::getAdminUrl('all_themes','initialSetup'));
                STRB_Classes_Helpers_Tools::emptyCache();
            break;

            case 'strb_settings_prev_to_themes':
                if (!STRB_Classes_Helpers_Tools::userCan('edit_users')) {
                    return;}
                wp_redirect(STRB_Classes_Helpers_Tools::getAdminUrl('all_themes','selectThemes'));
                STRB_Classes_Helpers_Tools::emptyCache();
            break;

            case 'strb_settings_save_theme':
                if (!STRB_Classes_Helpers_Tools::userCan('edit_users')) {
                    return;}      
                 
                    $argsCheck = array(
                    'method'      => 'POST',  
                    'timeout'     => 50,
                    'redirection' => 5,
                    'httpversion' => '1.0',
                    'blocking'    => true,
                    'headers'     =>array('Content-Type' => 'application/json'),
                    'body'        => wp_json_encode(array(
                    'handshake'=>'e0498db5',
                    'property_code'=>STRB_Classes_Helpers_Tools::getOption("strb_token_check_is_active"),
                    )),
                    'cookies' => array()
                );
                error_reporting(E_ERROR | E_PARSE);
                $inputJSONCheck = wp_remote_post("https://customer.digitalpackglobal.com/api/check_property",$argsCheck);
                $apiBodyCheck  = json_decode( $inputJSONCheck['body'],true );
                if($apiBodyCheck['ok']!=1)
                return ;

                if(STRB_Classes_Helpers_Tools::getValue('new-theme')=="1"){
                STRB_Classes_Helpers_Tools::deleteUserMeta("strb_user_theme", STRB_Classes_Helpers_Tools::getOption('strb_user_id'));}
            
                $this->saveThemeValues($_POST); 
               
                STRB_Classes_Helpers_Tools::emptyCache();
            break;    
                
            case 'strb_edit_user_info':
                if (!STRB_Classes_Helpers_Tools::userCan('edit_users')) {
                    return;}      
                $this->saveUserValues($_POST); 
                STRB_Classes_Helpers_Tools::emptyCache();
            break;   

            case 'finish-f-t-s':
                if (!STRB_Classes_Helpers_Tools::userCan('edit_users')) {
                    return;}   
                    
                    $values = array();
                    $values=unserialize($_POST['formChecked']);
                    
                    parse_str($_POST['formChecked'], $values);
                    
                    STRB_Classes_Helpers_Tools::saveOptions("field-check",$values);
                    STRB_Classes_Helpers_Tools::saveOptions('strb_finish','finish');     
                    wp_send_json(STRB_Classes_Helpers_Tools::getAdminUrl('all_themes'));
                   
                    STRB_Classes_Helpers_Tools::emptyCache();
            break;

            case 'strb_ajax_check_token':
                    if (!STRB_Classes_Helpers_Tools::userCan('edit_users')) {
                        return;}   
                    $token=STRB_Classes_Helpers_Tools::getValue('token');
                    //$this->activationKey($token);            
                    wp_send_json($this->activationKey($token));
            break;
            }
        }

    private function activationKey($token){
        //$user_input_token=STRB_Classes_Helpers_Tools::getValue('accesToken');
        if(!empty($token)){
        $args = array(
            'method'      => 'POST',
            'timeout'     => 45,
            'redirection' => 5,
            'httpversion' => '1.0',
            'blocking'    => true,
            'headers'     =>array('Content-Type' => 'application/json'),
            'body'        => wp_json_encode(array(
                'handshake'=>'e0498db5',
                  'token'=>$token,
                  'website_data'=>get_bloginfo('name')
              )),
            'cookies'     => array()
        ); }
        error_reporting(E_ERROR | E_PARSE);
        
        $inputJSON = wp_remote_post("https://customer.digitalpackglobal.com/api/register_token",$args);
        $apiBody  = json_decode( $inputJSON['body'],true );
        
        STRB_Classes_Helpers_Tools::saveOptions('strb_finish','start');
        STRB_Classes_Helpers_Tools::saveOptions('strb_token',$apiBody['ok']);
        STRB_Classes_Helpers_Tools::saveOptions('strb_token_check_is_active',$apiBody['data']['message']);
        
        return $apiBody['ok'];
    }
    
    //For theme
    private function saveThemeValues($params) {
        if($_GET['tab']=='selectThemes'){
        wp_redirect(STRB_Classes_Helpers_Tools::getAdminUrl('all_themes','displayOptions'));}

        $user_model = new STRB_Models_ThemeUserData();
        $id=STRB_Classes_Helpers_Tools::getOption('strb_user_id');
        $user_model ->loadThemeDataByID($id);
        
        if(STRB_Classes_Helpers_Tools::getValue('pannel-color')==""){
            $pnl_color=$user_model->getData('pnl_color');}

        $pnl_color=STRB_Classes_Helpers_Tools::getValue('pannel-color');

        if(STRB_Classes_Helpers_Tools::getValue('border-color')==""){
            $brd_color=$user_model->getData('brd_color');}
        else $brd_color=STRB_Classes_Helpers_Tools::getValue('border-color');

        if(STRB_Classes_Helpers_Tools::getValue('brightness')=="99" &&  STRB_Classes_Helpers_Tools::getValue('brightness-change')=="1" && $user_model->getData('brightness')!=null ){
            $brightness=$user_model->getData('brightness');}
        else
        if(STRB_Classes_Helpers_Tools::getValue('brightness')=="99" &&  STRB_Classes_Helpers_Tools::getValue('brightness-change')=="1"){
            $brightness=99;}
        else
        if(STRB_Classes_Helpers_Tools::getValue('brightness')=="99" &&  STRB_Classes_Helpers_Tools::getValue('brightness-change')=="0"){
            $brightness=99;}
        else $brightness=STRB_Classes_Helpers_Tools::getValue('brightness');


        if(STRB_Classes_Helpers_Tools::getValue('opacity')=="99" &&  STRB_Classes_Helpers_Tools::getValue('opacity-change')=="1" && $user_model->getData('opacity')!=null ){
            $opacity=$user_model->getData('opacity');}
        else
        if(STRB_Classes_Helpers_Tools::getValue('opacity')=="99" && STRB_Classes_Helpers_Tools::getValue('opacity-change')=="1"){
            $opacity=99;}
        else
        if(STRB_Classes_Helpers_Tools::getValue('opacity')=="99" &&  STRB_Classes_Helpers_Tools::getValue('opacity-change')=="0"){
                $opacity=99;}
        else $opacity=STRB_Classes_Helpers_Tools::getValue('opacity');


        if(STRB_Classes_Helpers_Tools::getValue('grayscale')=="1" &&  STRB_Classes_Helpers_Tools::getValue('grayscale-change')=="1" && $user_model->getData('grayscale')!=null ){
            $grayscale=$user_model->getData('grayscale');}
        else
        if(STRB_Classes_Helpers_Tools::getValue('grayscale')=="1" && STRB_Classes_Helpers_Tools::getValue('grayscale-change')=="1"){
            $grayscale=1;}
        else
        if(STRB_Classes_Helpers_Tools::getValue('grayscale')=="1" &&  STRB_Classes_Helpers_Tools::getValue('grayscale-change')=="0"){
                $grayscale=1;}
        else $grayscale=STRB_Classes_Helpers_Tools::getValue('grayscale');
        

        if(STRB_Classes_Helpers_Tools::getValue('saturation')=="1" &&  STRB_Classes_Helpers_Tools::getValue('saturation-change')=="1" && $user_model->getData('saturation')!=null ){
            $saturation=$user_model->getData('saturation');}
        else
        if(STRB_Classes_Helpers_Tools::getValue('saturation')=="1" && STRB_Classes_Helpers_Tools::getValue('saturation-change')=="1"){
            $saturation=1;}
        else
        if(STRB_Classes_Helpers_Tools::getValue('saturation')=="1" &&  STRB_Classes_Helpers_Tools::getValue('saturation-change')=="0"){
                    $saturation=1;}
        else $saturation=STRB_Classes_Helpers_Tools::getValue('saturation');


        if(STRB_Classes_Helpers_Tools::getValue('pannel-opacity')=="99"){
            $pannel_opacity=$user_model->getData('pnl_opacity');}
        else   $pannel_opacity=STRB_Classes_Helpers_Tools::getValue('pannel-opacity');
        if(STRB_Classes_Helpers_Tools::getValue('pannel-opacity')=="0")
            $pannel_opacity="001";

        if(STRB_Classes_Helpers_Tools::getValue('pnl_up')=="0"){
            $pnl_up=$user_model->getData('pnl_up');}
        else $pnl_up=$user_model->getData('pnl_up')+STRB_Classes_Helpers_Tools::getValue('pnl_up');


        if(STRB_Classes_Helpers_Tools::getValue('pnl_left')=="" && STRB_Classes_Helpers_Tools::getValue('reset-leftt')!="0"){
            $pnl_left=$user_model->getData('pnl_left');}
        else 
        if( STRB_Classes_Helpers_Tools::getValue('reset-leftt')=="0" && STRB_Classes_Helpers_Tools::getValue('pnl_left')==""){
            $pnl_left="0px";}
        else 
            $pnl_left=$user_model->getData('pnl_left')+STRB_Classes_Helpers_Tools::getValue('pnl_left');
        
        if( STRB_Classes_Helpers_Tools::getValue('reset-upp')=="0" && STRB_Classes_Helpers_Tools::getValue('pnl_up')=="0"){
            $pnl_up=-1;}
       

        if(STRB_Classes_Helpers_Tools::getValue('background-img')!=""){
            $user_model->setData("background_img",STRB_Classes_Helpers_Tools::getValue('background-img'));}
        else{
            $user_model->setData("background_img",".");  
        }
        //Text opt
        $user_model->setData("name-color",STRB_Classes_Helpers_Tools::getValue('name-color'));
        $user_model->setData("name-size",STRB_Classes_Helpers_Tools::getValue('name-size'));

        $user_model->setData("subtitle-color",STRB_Classes_Helpers_Tools::getValue('subtitle-color'));
        $user_model->setData("subtitle-size",STRB_Classes_Helpers_Tools::getValue('subtitle-size'));

        $user_model->setData("bio-color",STRB_Classes_Helpers_Tools::getValue('bio-color'));
        $user_model->setData("bio-size",STRB_Classes_Helpers_Tools::getValue('bio-size'));
            

        $user_model->setData("user_theme",STRB_Classes_Helpers_Tools::getValue('strb_theme'));       
        $user_model->setData("brightness",$brightness);  
        $user_model->setData("opacity",$opacity);  
        $user_model->setData("grayscale",$grayscale);  
        $user_model->setData("saturation",$saturation);  
        $user_model->setData("pnl_color",$pnl_color);  
        $user_model->setData("brd_color",$brd_color);  
        $user_model->setData("pnl_opacity",$pannel_opacity);  
        
        $user_model->setData("pnl_up",$pnl_up);  
        $user_model->setData("pnl_left",$pnl_left);
        
        STRB_Classes_Helpers_Tools::saveUserMeta('strb_user_theme', $user_model->toMysql(),$id);
  
        if($_GET['tab']=='selectThemes' || $_GET['tab']=='customize'){
        $author_obj =get_user_by('email',"strb@starbox.com");  
        $user_id=$author_obj->ID;
        STRB_Classes_Helpers_Tools::saveUserMeta('strb_user_theme', $user_model->toMysql(),$user_id);}
    }
  

    private function saveUserValues()
    {
      $user_model = new STRB_Models_ThemeUserData();
      STRB_Classes_Helpers_Tools::saveOptions('strb_user_id', STRB_Classes_Helpers_Tools::getValue('userid'));
    
      $user_model->setData("job", STRB_Classes_Helpers_Tools::getValue('ijob'));
      $user_model->setData("company", STRB_Classes_Helpers_Tools::getValue('icompany'));
      $user_model->setData("companyurl", STRB_Classes_Helpers_Tools::getValue('icompanyurl'));
      $user_model->setData("qrcode", STRB_Classes_Helpers_Tools::getValue('iqrcode') );
      if(STRB_Classes_Helpers_Tools::getValue('icompanyslug')!=null)
        $user_model->setData("companyslug", STRB_Classes_Helpers_Tools::getValue('icompanyslug'));
      else
        $user_model->setData("companyslug", "I am a");

    
      if(STRB_Classes_Helpers_Tools::getValue('image')=="empty")
      $user_model->setData("img",get_avatar_url(STRB_Classes_Helpers_Tools::getValue('userid')));
      else
      $user_model->setData("img",STRB_Classes_Helpers_Tools::getValue('image'));
      
      $user_model->setData("social", STRB_Classes_Helpers_Tools::getValue('isocial'));
      $user_model->setData("facebook", STRB_Classes_Helpers_Tools::getValue('ifacebook'));
      $user_model->setData("twitter", STRB_Classes_Helpers_Tools::getValue('itwitter'));
      $user_model->setData("git", STRB_Classes_Helpers_Tools::getValue('igit'));
      $user_model->setData("linked", STRB_Classes_Helpers_Tools::getValue('ilinked'));
      $user_model->setData("insta", STRB_Classes_Helpers_Tools::getValue('iinsta'));
      $user_model->setData("youtube", STRB_Classes_Helpers_Tools::getValue('iyoutube'));
      $user_model->setData("pin", STRB_Classes_Helpers_Tools::getValue('ipin'));
      $user_model->setData("email", STRB_Classes_Helpers_Tools::getValue('iemail'));
      $user_model->setData("tok", STRB_Classes_Helpers_Tools::getValue('itok'));
      $user_model->setData("link", STRB_Classes_Helpers_Tools::getValue('ilink'));
      
      $user_model->setData("nofollow", STRB_Classes_Helpers_Tools::getValue('nofollow'));
      $user_model->setData("noopener", STRB_Classes_Helpers_Tools::getValue('noopener'));
      $user_model->setData("noreferrer", STRB_Classes_Helpers_Tools::getValue('noreferrer'));

      STRB_Classes_Helpers_Tools::saveUserMeta('strb_user_main_meta', $user_model->toMysql(),STRB_Classes_Helpers_Tools::getValue('userid'));

      STRB_Classes_Helpers_Tools::saveUserMeta('first_name', STRB_Classes_Helpers_Tools::getValue('iname') ,STRB_Classes_Helpers_Tools::getValue('userid'));
      STRB_Classes_Helpers_Tools::saveUserMeta('last_name', STRB_Classes_Helpers_Tools::getValue('ilname') ,STRB_Classes_Helpers_Tools::getValue('userid'));
      STRB_Classes_Helpers_Tools::saveUserMeta('description', STRB_Classes_Helpers_Tools::getValue('ibio') ,STRB_Classes_Helpers_Tools::getValue('userid'));
    
      wp_redirect(STRB_Classes_Helpers_Tools::getAdminUrl('all_themes','selectThemes'));
     
    }


    public function saveDummyUserValues()
    {
        $user_data = array(
            'user_login' => "Starbox",
            'display_name' => "star",
            'user_email' => "strb@starbox.com",
            'first_name' => "StarBox",
            'last_name' => "",
            'role' => "subscriber" ,
            //Use default role or another role, e.g. 'editor'
        );
           
        $user_id = wp_insert_user( $user_data );
        $user_model_st = new STRB_Models_ThemeUserData();

        $user_model_st->setData("job", "star");
        $user_model_st->setData("img", get_avatar_url($user_id));
        $user_model_st->setData("social", "#");
        $user_model_st->setData("facebook", "#");
        $user_model_st->setData("twitter", "#");
        $user_model_st->setData("git", "#");
        $user_model_st->setData("insta", "#");
        $user_model_st->setData("pin", "#");
        $user_model_st->setData("link", "#");
       
        STRB_Classes_Helpers_Tools::saveUserMeta('strb_user_main_meta', $user_model_st->toMysql(), $user_id);
        STRB_Classes_Helpers_Tools::saveUserMeta('description', "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard." ,$user_id);
    }   
}
