<?php
namespace Formipay;
use Formipay\Traits\SingletonTrait;

if ( ! defined( 'ABSPATH' ) ) exit;

class Settings {

    use SingletonTrait;

	/**
	 * Initializes the plugin by setting filters and administration functions.
	 */
    
    protected function __construct() {

        add_filter( 'wpcfto_options_page_setup',    [$this, 'theme_option'] );
        add_action( 'admin_enqueue_scripts',        [$this, 'enqueue'] );

    }

    public function theme_option($setups){

        $gateways = apply_filters( 'formipay/form-config/tab:payments/gateways', [] );

        $payment_checkboxes = [];
        if(!empty($gateways)){
            foreach($gateways as $gateway){
                $id = $gateway['id'];
                $label = $gateway['gateway'];
                if(isset($gateway['channel'])){
                    $label .= ' - '.$gateway['channel'];
                }
                $payment_checkboxes[$id] = $label;
            }
        }

        $general_fields = array(
            'business_group' => array(
                'type' => 'group_title',
                'label' => __( 'Business', 'formipay' ),
                'group' => 'started'
            ),
            'business_name' => array(
                'type' => 'text',
                'label' => __( 'Business Name', 'formipay' ),
                'description' => __( 'This may be displayed on payment gateway like Paypal as a merchant name.', 'formipay' ),
                'required' => true,
                'group' => 'ended'
            ),
            'currency_group' => array(
                'type' => 'group_title',
                'label' => __( 'Currency', 'formipay' ),
                'group' => 'started'
            ),
            'enable_multicurrency' => [
                'type' => 'checkbox',
                'label' => __( 'Enable Multi Currency', 'formipay' )
            ],
            'enable_auto_exchangerate' => [
                'type' => 'checkbox',
                'label' => __( 'Enable Auto Exchange Rate', 'formipay' ),
                'dependency' => [
                    'key' => 'enable_multicurrency',
                    'value' => 'not_empty'
                ]
            ],
            'enable_auto_exchangerate_apikey' => [
                'type' => 'text',
                'label' => __( 'Auto Exchange Rate API Key', 'formipay' ),
                'required' => true,
                'dependency' => [
                    [
                        'key' => 'enable_multicurrency',
                        'value' => 'not_empty'
                    ],
                    [
                        'key' => 'enable_auto_exchangerate',
                        'value' => 'not_empty'
                    ]
                ],
                'dependencies' => '&&'
            ],
            'multicurrencies' => [
                'type' => 'repeater',
                'label' => __( 'Currencies', 'formipay' ),
                'fields' => [
                    'currency' => array(
                        'type' => 'select',
                        'label' => __('Default Currency', 'formipay'),
                        'value' => 'IDR:::Indonesian rupiah:::Rp',
                        'options' => formipay_currency_as_options(),
                        'required'      => true,
                        'searchable' => true,
                        'is_group_title' => true
                    ),
                    'decimal_digits' => array(
                        'type' => 'number',
                        'label' => __('Decimal Digits', 'formipay'),
                        'value' => '2',
                        'required'      => true,
                    ),
                    'decimal_symbol' => array(
                        'type' => 'text',
                        'label' => __('Decimal Symbol', 'formipay'),
                        'value' => '.',
                        'required'      => true,
                    ),
                    'thousand_separator' => array(
                        'type' => 'text',
                        'label' => __('Thousand Separator Symbol', 'formipay'),
                        'value' => ',',
                        'required'      => true,
                    ),
                    'payment_gateways' => array(
                        'type' => 'multi_checkbox',
                        'label' => __( 'Payment Gateways', 'formipay' ),
                        'options' => $payment_checkboxes,
                        'submenu' => __( 'General', 'formipay' ),
                    ),
                    'exchange_rate' => array(
                        'type' => 'number',
                        'label' => __( 'Manual Exchange Rate', 'formipay' ),
                        'description' => __( 'This value is the exchange rate of default currency against this currency. If this currency selected, total order will be multiplied to this value. <b>This override the value from ExchangeRatePI if enabled</b>', 'formipay' ),
                        'submenu' => __( 'General', 'formipay' ),
                    )
                ],
                'required' => true,
                'dependency' => [
                    'key' => 'enable_multicurrency',
                    'value' => 'not_empty'
                ]
            ],
            'default_currency' => array(
                'type' => 'select',
                'label' => __('Default Currency', 'formipay'),
                'value' => 'IDR:::Indonesian rupiah:::Rp',
                'options' => formipay_currency_as_options(),
                'required' => true,
                'searchable' => true
            ),
            'default_currency_decimal_digits' => array(
                'type' => 'number',
                'label' => __('Decimal Digits', 'formipay'),
                'value' => '2',
                'required'      => true,
                'dependency' => [
                    'key' => 'enable_multicurrency',
                    'value' => 'empty'
                ]
            ),
            'default_currency_decimal_symbol' => array(
                'type' => 'text',
                'label' => __('Decimal Symbol', 'formipay'),
                'value' => '.',
                'required'      => true,
                'dependency' => [
                    'key' => 'enable_multicurrency',
                    'value' => 'empty'
                ]
            ),
            'default_currency_thousand_separator' => array(
                'type' => 'text',
                'label' => __('Thousand Separator Symbol', 'formipay'),
                'value' => ',',
                'required'      => true,
                'dependency' => [
                    'key' => 'enable_multicurrency',
                    'value' => 'empty'
                ],
                'group' => 'ended'
            ),
        );

        $general_fields = apply_filters( 'formipay/global-settings/tab:general', $general_fields );

        $pages_fields = array(

            'thankyou_page_group' => array(
                'type' => 'group_title',
                'label' => __( 'Thank-You Page Style', 'formipay' ),
                'submenu' => __( 'Thank-You Page', 'formipay' ),
                'group' => 'started'
            ),
            'thankyou_link' => array(
                'type' => 'text',
                'label' => __( 'Thank-You Page Link', 'formipay' ),
                'value' => 'thankyou',
                'submenu' => __( 'Thank-You Page', 'formipay' ),
                'required' => true
            ),
            'thankyou_style' => array(
                'type' => 'image_select',
                'label' => esc_html__( 'Style', 'formipay' ),
                'width' => 100,
                'height' => 100,
                'value' => 'receipt',
                'options' => array(
                    'card' => array(
                        'alt'   => 'Card',
                        'img'   => FORMIPAY_URL . 'admin/assets/img/thankyou_card_style.png'
                    ),
                    'receipt' => array(
                        'alt'   => 'Receipt',
                        'img'   => FORMIPAY_URL . 'admin/assets/img/thankyou_receipt_style.png'
                    ),
                ),
                'submenu' => __( 'Thank-You Page', 'formipay' ),
                'required' => true
            ),
            'thankyou_page_container_bg_color' => array(
                'type' => 'color',
                'label' => __( 'Container Background Color', 'formipay' ),
                'value' => '#808080',
                'description' => __( 'Container is the main div on Thank-You Page contents', 'formipay' ),
                'submenu' => __( 'Thank-You Page', 'formipay' ),
            ),
            'thankyou_page_wrapper_bg_color' => array(
                'type' => 'color',
                'label' => __( 'Wrapper Background Color', 'formipay' ),
                'value' => '#ffffff',
                'description' => __( 'Wrapper is the div that fit to Thank-You Page contents width', 'formipay' ),
                'submenu' => __( 'Thank-You Page', 'formipay' ),
            ),
            'thankyou_page_wrapper_max_width' => array(
                'type' => 'number',
                'label' => __( 'Wrapper Max Width', 'formipay' ),
                'value' => '600',
                'submenu' => __( 'Thank-You Page', 'formipay' ),
                'group' => 'ended',
                'required' => true
            ),
            'thankyou_page_restriction_group' => array(
                'type' => 'group_title',
                'label' => __( 'Restriction Access', 'formipay' ),
                'submenu' => __( 'Thank-You Page', 'formipay' ),
                'group' => 'started'
            ),
            'thankyou_page_restriction_thumbnail' => array(
                'type' => 'image',
                'label' => __( 'Thumbnail', 'formipay' ),
                'submenu' => __( 'Thank-You Page', 'formipay' )
            ),
            'thankyou_page_restriction_title' => array(
                'type' => 'text',
                'label' => __( 'Title', 'formipay' ),
                'value' => __( 'Request to Access', 'formipay' ),
                'submenu' => __( 'Thank-You Page', 'formipay' )
            ),
            'thankyou_page_restriction_message' => array(
                'type' => 'hint_textarea',
                'label' => __( 'Message', 'formipay' ),
                'value' => __( 'Input your {{media}} to get new access link.', 'formipay' ),
                'submenu' => __( 'Thank-You Page', 'formipay' ),
                'hints' => array(
                    'media' => __( 'Contact Media', 'formipay' ) 
                ),
                'description' => __( 'Use {{media}} shortcode to define what media of contact the buyer can receive the access link.', 'formipay' )
            ),
            'thankyou_page_restriction_button' => array(
                'type' => 'text',
                'label' => __( 'Request Access Button', 'formipay' ),
                'value' => __( 'Get Access Link', 'formipay' ),
                'submenu' => __( 'Thank-You Page', 'formipay' ),
                'group' => 'ended'
            )
        );

        $pages_fields = apply_filters( 'formipay/global-settings/tab:pages', $pages_fields );

        $global = array(
            'General' => array(
                'name' => __( 'General', 'formipay' ),
                'fields' => $general_fields
            ),
            'Pages' => array(
                'name' => __( 'Pages', 'formipay' ),
                'fields' => $pages_fields
            )
        );

        $global = apply_filters( 'formipay/global-settings', $global );

        foreach($global as $key => $value){
            $fields[$key] = $value;
        }

        $setups[] = array(
            'option_name'   => 'formipay_settings',
            'title'         => __('Formipay', 'formipay'),
            'sub_title'     => __('Settings', 'formipay'),
            'logo'          => FORMIPAY_URL . 'admin/assets/img/formipay-logo-circle-white.png',
            'page'          => array(
                'parent_slug'   => 'formipay',
                'page_title'    => __('Formipay Settings', 'formipay'),
                'menu_title'    => __('Settings', 'formipay'),
                'menu_slug'     => 'formipay-settings',
                'position'      => 40,
            ),
    
            'fields' => $fields
        );
    
        return $setups;
    }

    public function enqueue() {
        
        global $current_screen;

        if ( $current_screen->id === 'formipay_page_formipay-settings' ) {
            wp_enqueue_style('admin-setting-style', FORMIPAY_URL . 'admin/assets/css/global-setting.css', [], FORMIPAY_VERSION, 'all' );
            wp_enqueue_script('admin-setting-script', FORMIPAY_URL . 'admin/assets/js/admin-setting.js', ['jquery'], FORMIPAY_VERSION, true);

            wp_localize_script( 'admin-setting-script', 'formipay_admin_setting', [
                'ajax_url'  => admin_url('admin-ajax.php'),
                'site_url'  => site_url(),
                'nonce'     => wp_create_nonce('formipay-admin-nonce'),
                'multicurrency' => formipay_is_multi_currency_active(),
                'all_currencies' => formipay_currency_as_options(),
                'global_selected_currencies' => formipay_global_currency_options(),
                'default_currency' => formipay_default_currency()
            ] );
        }
    }

}