<?php

namespace RespacioHouzezImport;
use RespacioHouzezImport\remote;
class option{
    //this to get option value of log per page
    public static function getLogPerPageOption(){
        if(get_option( 'RespacioHouzezImportLogPerPage', false) == false){
            self::setLogPerPageOption(10);
        }
        return get_option( 'RespacioHouzezImportLogPerPage', false);
    }

    //this to set option value of log per page
    public static function setLogPerPageOption($value){
        
        return update_option( 'RespacioHouzezImportLogPerPage', $value);
    }
    
    //This to return crm base path
    public static function getCrmBasePath(){
        //checking if the option value empty or not 
        $optionValue = get_option('RespacioHouzezImportCrmBasePath',false);
        //if not empty return it
        if($optionValue != false) {
            return $optionValue;
        }
        //set it
        self::setCrmBasePath();

        //returning the option value 
        return get_option('RespacioHouzezImportCrmBasePath',false);
    }

    //This to set crm base path
    public static function setCrmBasePath( $path = false){
        //if no path provided use default path
        if($path == false) $path = 'crm.respacio.com';

        //sanitize before insertion 
        $path = sanitize_text_field($path);

        //inserting to the option table 
        //returning bool
        return update_option('RespacioHouzezImportCrmBasePath',$path);
    }

    //getting email controller path
    public static function getEmailControllerPath(){
        $optionValue = get_option('RespacioHouzezImportEmailControllerPath',false);
        if($optionValue != false){
            return $optionValue;
        }
        //setting it if not found
        self::setEmailControllerPath();

        //no need to unserialize 
        return get_option('RespacioHouzezImportEmailControllerPath',false);

    }

    //setting email controller path
    public static function setEmailControllerPath(array $paths = []){
        
        //checking if provided $paths are empty or not 
        if(empty($paths)) {
            $paths = array(
                'website_control',
                'send_verification_code'
            );
        }
        //sanitizing 
        foreach($paths as &$path){
            $path = sanitize_text_field($path);
        }
        //no need to be serialized, it will do by itself
        return update_option('RespacioHouzezImportEmailControllerPath',$paths);
    }


    public static function getCreateAccountPath(){
        $optionValue = get_option('RespacioHouzezImportCreateAccountPath',false);
        if($optionValue != false){
            return $optionValue;
        }
        //setting it if not found
        self::setCreateAccountPath();
        //no need to unserialize 
        return get_option('RespacioHouzezImportCreateAccountPath',false);

    }


    public static function setCreateAccountPath(array $paths = []){
        //checking if provided $paths are empty or not 
        if(empty($paths)) {
            $paths = array(
                'website_control',
                'user_signup'
            );
        }
        //sanitizing 
        foreach($paths as &$path){
            $path = sanitize_text_field($path);
        }
        //no need to be serialized, it will do by itself
        return update_option('RespacioHouzezImportCreateAccountPath',$paths);
    }

    public static function getRespacioSignature(){
        $optionValue = get_option('RespacioHouzezImportSignature',false);
        if($optionValue != false){
            return $optionValue;
        }
        //setting it if not found
        self::setRespacioSignature();
        return get_option('RespacioHouzezImportSignature',false);
    }

    public static function setRespacioSignature(){
        //generating
        $random  = wp_generate_password(32, false);
        return update_option('RespacioHouzezImportSignature',$random);
    }


    public static function getAccountLoginPath(){
        $optionValue = get_option('RespacioHouzezImportAccountLoginPath',false);
        if($optionValue != false){
            return $optionValue;
        }
        //setting it if not found
        self::setAccountLoginPath();

        //no need to unserialize 
        return get_option('RespacioHouzezImportAccountLoginPath',false);
    }

    public static function setAccountLoginPath(){
        //checking if provided $paths are empty or not 
        if(empty($paths)) {
            $paths = array(
                'website_control',
                'login'
            );
        }
        //sanitizing 
        foreach($paths as &$path){
            $path = sanitize_text_field($path);
        }
        //no need to be serialized, it will do by itself
        return update_option('RespacioHouzezImportAccountLoginPath',$paths);
    }

    /**
     * getting api key
     * @param void
     * @return false|mixed|null
     */
    public static function getApiKey(){
        return get_option('property_verification_api',false);
    }

    /**
     * setting new api key
     * @param string $key
     * @return bool
     */
    public static function setApiKey(string $key=''){
        if($key == '') return false;
        update_option('verify_api',true,true);
        return update_option('property_verification_api',$key);
    }

    /**
     * this to sync the form fields with crm. which include custom and default
     * @param void
     * @return boolean
     */
    public static function syncCrmFormFields(){
        $default = [
            ['name'=>'First Name', 'parameter_key' => 'name'],
            ['name'=>'Last Name', 'parameter_key' => 'surname'],
            ['name'=>'Email','parameter_key' => 'email_address'],
            ['name'=>'Phone','parameter_key' => 'phone_number'],
            ['name'=>'Notes','parameter_key' => 'notes'],
            ['name'=>'Url','parameter_key' => 'cla_detecturl'],
            ['name'=>'Prop ref number', 'parameter_key' => 'property_reference_number'],
            ['name'=>'Address line', 'parameter_key' => 'address'],            
            ['name'=>'City', 'parameter_key' => 'cla_city'],
            ['name'=>'Country_cla', 'parameter_key' => 'cla_country'],
            ['name'=>'Device', 'parameter_key' => 'cla_device'],
            ['name'=>'Region', 'parameter_key' => 'cla_region'],
            ['name'=>'Country', 'parameter_key' => 'country'],
            ['name'=>'Nationality', 'parameter_key' => 'nationality'],
            ['name'=>'Group Name', 'parameter_key' => 'group_name'],
            ['name'=>'Source', 'parameter_key' => 'source'],
            ['name'=>'Property For', 'parameter_key' => 'property_for'],
            ['name'=>'Rental period', 'parameter_key' => 'rental_period'],
            ['name'=>'Date Earliest', 'parameter_key' => 'from_date_earliest'],
            ['name'=>'Date Latest', 'parameter_key' => 'from_date_latest'],
            ['name'=>'Minimum Bedroom', 'parameter_key' => 'min_bedroom'],
            ['name'=>'Minimum Price', 'parameter_key' => 'min_price'],
            ['name'=>'Maximum Price', 'parameter_key' => 'max_price'],
            ['name'=>'Rent Period', 'parameter_key' => 'rent_period'],
            ['name'=>'Adult', 'parameter_key' => 'adult'],
            ['name'=>'Sales Person Id', 'parameter_key' => 'sales_person_id'],
            ['name'=>'From Date', 'parameter_key' => 'from_date'],
            ['name'=>'To Date', 'parameter_key' => 'to_date'],
            ['name'=>'Rent Type', 'parameter_key' => 'rent_type'],
            ['name'=>'Preferred Location', 'parameter_key' => 'preferred_location'],
            ['name'=>'Is Automation Deal', 'parameter_key' => 'is_automation_deal'],
            ['name'=>'Is Vendor ', 'parameter_key' => 'is_vendor ']
        ];
        $customFields = remote::get(
            'https://crm.respacio.com/ws/properties/getcustomfield',
            ['x-api-key' => self::getApiKey()]
        );

        if(is_wp_error($customFields)) return $customFields->get_error_message();
        $customFields = json_decode($customFields,true);
        $customFields = $customFields['data']['customfield'];


        //removing id
        $newCustomFields = [];
        foreach($customFields as $customField){
            $newCustomFields[] = ['name' => $customField['name'],'parameter_key' => $customField['parameter_key']];
        }

       //attributes
        $attributeFields = remote::get(
            RHIMO_API_BASE_URL.'/get_attributes',
            ['x-api-key' => self::getApiKey()]
        );

        if(is_wp_error($attributeFields)) return $attributeFields->get_error_message();
        $attributeFields = json_decode($attributeFields,true);
        $attributeFields = $attributeFields['attrbiute_list'];


        $newAttributeFields = [];
        foreach($attributeFields as $attributeField){
            $newAttributeFields[] = ['name' => $attributeField['attribute'],'parameter_key' => 'attribute_'.$attributeField['attribute_id']];
        }

        $newCustomFieldsAndAttributes = array_merge($default,$newCustomFields,$newAttributeFields);

        return update_option('respacio_houzez_custom_fields',$newCustomFieldsAndAttributes);
    }

    public static function getCrmFromFields(){
        /*$value = get_option('respacio_houzez_custom_fields',false);
        if($value){
            return $value;
        }*/
        self::syncCrmFormFields();
        return get_option('respacio_houzez_custom_fields',false);
    }
}