<?php

namespace KirilKirkov\WpInvoices;

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

class Config
{
    const PREMIUM_LINK = 'https://codecanyon.net/item/wp-invoices-pdf-electronic-invoicing-system/36891583';

    const DEFAULT_DATE_FORMAT = 'dd.mm.yy';

    const SETTINGS_GET_PARAM = 'kirilkirkov-pdf-invoice-manager-settings';
    const INPUTS_PREFIX = 'kirilkirkovwipdf_'; // kkwi -> kirilkirkov wordpress invoices
    const SCRIPTS_PREFIX = 'kirilkirkovwipdf-';

    // settings page options
    const MAIN_INPUT_GROUP = 'kirilkirkovwipdf-wp-invoices-inv-update-options';
    const CLIENTS_PAGE_INPUT_GROUP = 'kirilkirkovwipdf-wp-invoices-clients-update-options';
    const COMPANY_INPUT_GROUP = 'kirilkirkovwipdf-wp-invoices-company-update-options';
    const TEMPLATES_INPUT_GROUP = 'kirilkirkovwipdf-wp-invoices-templates-update-options';
    const WOOCOMMERCE_INPUT_GROUP = 'kirilkirkovwipdf-wp-invoices-woocommerce-update-options';
    const PAYMENTS_INPUT_GROUP = 'kirilkirkovwipdf-wp-invoices-payments-update-options';
    const CURRENCY_EXCHANGE_INPUT_GROUP = 'kirilkirkovwipdf-wp-invoices-currency-exchange-update-options';
    const API_INTEGRATION_INPUT_GROUP = 'kirilkirkovwipdf-wp-invoices-api-integration-update-options';

    /**
     * top level pages
     */
    const TOP_LEVEL_INVOICES_PAGE_SLUG = 'kirilkirkov-pdf-invoice-manager';
    const SUB_MENU_CREATE_INVOICE_PAGE_SLUG = 'kirilkirkov-wp-invoice-create';
    const SUB_MENU_PERIODIC_INVOICES_PAGE_SLUG = 'kirilkirkov-wp-periodic-invoices';
    const SUB_MENU_PERIODIC_INVOICES_EDIT_PAGE_SLUG = 'kirilkirkov-wp-periodic-invoices-edit';
    const SUB_MENU_PREVIEW_INVOICE_PAGE_SLUG = 'kirilkirkov-wp-invoice-preview';
    const SUB_MENU_SEND_INVOICE_PAGE_SLUG = 'kirilkirkov-wp-invoice-send';
    const SUB_MENU_CHARTS_INVOICE_PAGE_SLUG = 'kirilkirkov-wp-invoice-charts';
    const SUB_MENU_CLIENTS_PAGE_SLUG = 'kirilkirkov-wp-invoice-clients';
    const SUB_MENU_CREATE_CLIENT_PAGE_SLUG = 'kirilkirkov-wp-invoice-client-create';
    const SUB_MENU_ITEMS_PAGE_SLUG = 'kirilkirkov-wp-invoice-items';
    const SUB_MENU_CREATE_ITEM_PAGE_SLUG = 'kirilkirkov-wp-invoice-item-create';

    // used input fields with group type
    const GROUPS_INPUT_FIELDS = [
        self::MAIN_INPUT_GROUP => [
            'default_vat_percent',
            'default_vat_reason',
            'default_no_vat',
            'default_discount',
            'default_discount_type',
            'default_remarks',
            'count_decimal_digits',
            'disabled_js_calc',
            'currencies',
            'quantity_types',
            'payment_methods',
            'invoices_num_length',
            'disabled_items_update_quantity_on_edit',
            'disabled_items_update_quantity_on_create',
            'show_schiffer_field_in_invoices',
            'dates_format_type',
            'numbers_format_system',
            'currency_placement',
            'disable_auto_fill_compiled_field',
            'disalbe_origin_inv_generator',
        ],
        self::CLIENTS_PAGE_INPUT_GROUP => [
            'enable_shortcode_prev_inv',
        ],
        self::COMPANY_INPUT_GROUP => [
            'company_name',
            'company_vat_registered',
            'company_vat_number',
            'company_address',
            'company_city',
            'company_accountable_person',
            'company_zip_code',
            'logo_file',
            'hide_logo_from_invoices',
        ],
        self::TEMPLATES_INPUT_GROUP => [
            'current_template'
        ],
        self::WOOCOMMERCE_INPUT_GROUP => [
            'auto_create_inv',
            'create_inv_when',
        ],
        self::PAYMENTS_INPUT_GROUP => [
            'wc_mark_order_as_paid_when_invoice_is_paid',
        ],
        self::CURRENCY_EXCHANGE_INPUT_GROUP => [
        ],
        self::API_INTEGRATION_INPUT_GROUP => [
            'api_enabled',
            'api_token',
        ]
    ];

    // load css/js in specific pages only!
    const LOAD_ASSETS_IN_PAGES = [
        self::SETTINGS_GET_PARAM,
        self::TOP_LEVEL_INVOICES_PAGE_SLUG,
        self::SUB_MENU_CREATE_INVOICE_PAGE_SLUG,
        self::SUB_MENU_PREVIEW_INVOICE_PAGE_SLUG,
        self::SUB_MENU_CHARTS_INVOICE_PAGE_SLUG,
        self::SUB_MENU_CLIENTS_PAGE_SLUG,
        self::SUB_MENU_CREATE_CLIENT_PAGE_SLUG,
        self::SUB_MENU_ITEMS_PAGE_SLUG,
        self::SUB_MENU_CREATE_ITEM_PAGE_SLUG,
        self::SUB_MENU_SEND_INVOICE_PAGE_SLUG,
        self::SUB_MENU_PERIODIC_INVOICES_EDIT_PAGE_SLUG,
    ];
    
    public static function get_groups_input_fieds()
    {
        return self::GROUPS_INPUT_FIELDS;
    }

    public static function is_current_screen_in_pages($screen_base)
    {
        foreach(self::LOAD_ASSETS_IN_PAGES as $page) {
            if(strpos($screen_base, $page) !== false) {
                return true;
            }
        }
        return false;
    }

    public static function get_invoice_types()
    {
        return array (
            'pro_forma' => esc_html__( 'Pro-forma', 'kirilkirkov-pdf-invoice-manager' ),
            'invoice' => esc_html__( 'Invoice', 'kirilkirkov-pdf-invoice-manager' ),
            'debit_note' => esc_html__( 'Debit note', 'kirilkirkov-pdf-invoice-manager' ),
            'credit_note' => esc_html__( 'Credit note', 'kirilkirkov-pdf-invoice-manager' ),
        );
    }

    public static function get_invoice_paid_types()
    {
        return array (
            '0' => esc_html__( 'Unpaid', 'kirilkirkov-pdf-invoice-manager' ),
            '1' => esc_html__( 'Paid', 'kirilkirkov-pdf-invoice-manager' ),
            '2' => esc_html__( 'Partly-Paid', 'kirilkirkov-pdf-invoice-manager' ),
        );
    }

    public static function get_invoice_statuses()
    {
        return array (
            '0' => esc_html__( 'Issued', 'kirilkirkov-pdf-invoice-manager' ),
            '1' => esc_html__( 'Send', 'kirilkirkov-pdf-invoice-manager' ),
            '2' => esc_html__( 'Draft', 'kirilkirkov-pdf-invoice-manager' ),
        );
    }

    /**
     * PDF Download types - copy/original
     */
    public static function get_inv_dwn_types()
    {
        return array (
            'original' => esc_html__( 'Original', 'kirilkirkov-pdf-invoice-manager' ),
            'copy' => esc_html__( 'Copy', 'kirilkirkov-pdf-invoice-manager' ),
        );
    }

    /**
     * Get translatable from php months names
     */
    public static function get_months()
    {
        return array(
            'Jan' => esc_html__('Jan', 'kirilkirkov-pdf-invoice-manager'),
            'Feb' => esc_html__('Feb', 'kirilkirkov-pdf-invoice-manager'),
            'Mar' => esc_html__('Mar', 'kirilkirkov-pdf-invoice-manager'),
            'Apr' => esc_html__('Apr', 'kirilkirkov-pdf-invoice-manager'),
            'May' => esc_html__('May', 'kirilkirkov-pdf-invoice-manager'),
            'Jun' => esc_html__('Jun', 'kirilkirkov-pdf-invoice-manager'),
            'Jul' => esc_html__('Jul', 'kirilkirkov-pdf-invoice-manager'),
            'Aug' => esc_html__('Aug', 'kirilkirkov-pdf-invoice-manager'),
            'Sep' => esc_html__('Sep', 'kirilkirkov-pdf-invoice-manager'),
            'Oct' => esc_html__('Oct', 'kirilkirkov-pdf-invoice-manager'),
            'Nov' => esc_html__('Nov', 'kirilkirkov-pdf-invoice-manager'),
            'Dec' => esc_html__('Dec', 'kirilkirkov-pdf-invoice-manager'),
        );
    }

    public static function get_dates_format_type($js = false)
    {
        $format = self::DEFAULT_DATE_FORMAT;

        $dates_format_type = get_option(Config::INPUTS_PREFIX.'dates_format_type');
        if($dates_format_type && in_array($dates_format_type, self::get_dates_format_types())) {
            $format = $dates_format_type;
        }

        if($js === true) {
            return $format;
        }

        $deliver = '/';
        $exp = explode('/', $format);
        if(count($exp) < 3) {
            $deliver = '.';
            $exp = explode('.', $format);
        }

        $final = '';
        foreach($exp as $e) {
            $out = array();
            for($i=0; $i < strlen($e); $i++){
                $out[$e[$i]] = $e[$i];
            }
            $final .= implode('', $out) . $deliver;
        }
        

        return rtrim(str_replace('y', 'Y', $final), $deliver);
    }

    public static function get_dates_format_types()
    {
        return array(
            'dd.mm.yy',
            'dd/mm/yy',

            'mm.dd.yy',
            'mm/dd/yy',
            
            'yy.mm.dd',
            'yy/mm/dd',

            'yy.dd.mm',
            'yy/dd/mm',
        );
    }

    public static function get_date_format_in_sql()
    {
        $current_format = self::get_dates_format_type(true);
        
        if($current_format === 'dd.mm.yy') {
            return '%%d.%%m.%%Y';
        } elseif($current_format === 'dd/mm/yy') {
            return '%%d/%%m/%%Y';
        } elseif($current_format === 'mm.dd.yy') {
            return '%%m.%%d.%%Y';
        } elseif($current_format === 'mm/dd/yy') {
            return '%%m/%%d/%%Y';
        } elseif($current_format === 'yy.mm.dd') {
            return '%%Y.%%m.%%d';
        } elseif($current_format === 'yy/mm/dd') {
            return '%%Y/%%m/%%d';
        } elseif($current_format === 'yy.dd.mm') {
            return '%%Y.%%d.%%m';
        } elseif($current_format === 'yy/dd/mm') {
            return '%%Y/%%d/%%m';
        } else {
            return null;
        }
    }

    public static function get_numbers_format_system()
    {
        $kirilkirkovwipdf_numbers_format_system_ = array('.', ',');
        $numbers_format_system = get_option(Config::INPUTS_PREFIX.'numbers_format_system');
        if($numbers_format_system && $numbers_format_system === 'metric') {
            $kirilkirkovwipdf_numbers_format_system_ = array(',', '.');
        }

        return $kirilkirkovwipdf_numbers_format_system_;
    }

    public static function is_string_float($string)
    {
        if(is_numeric($string)) {
            $val = $string+0;
    
            return is_float($val);
        } 
          
        return false;
    }

    /**
     * Get the amount and currency placement
     * 
     * @param string $amount
     * @param string $currency
     */
    public static function get_amount_currency_placement($amount, $currency)
    {
        $amount = $amount;
        $currency = $currency;

        $currency_placement = get_option(Config::INPUTS_PREFIX.'currency_placement');
        if(!$currency_placement || ($currency_placement === '' || $currency_placement === 'default')) {
            return $amount . ' ' . $currency;
        }

        if($currency_placement === 'currency_last__no_space') {
            return $amount . $currency;
        }

        if($currency_placement === 'currency_first__space') {
            return $currency . ' ' . $amount;
        }

        if($currency_placement === 'currency_first__no_space') {
            return $currency . $amount;
        }

        return $amount . ' ' . $currency;
    }

    public static function get_selected_currency()
    {
        $currencies = get_option(self::INPUTS_PREFIX.'currencies');
        $selected_currency = esc_html__('EUR', 'kirilkirkov-pdf-invoice-manager');
        if($currencies && is_array($currencies) && count($currencies)) {
            $selected_currency = reset($currencies); // always get first ordered from settings
        }

        return $selected_currency;
    }
}