<?php

namespace WKO\Controllers;

final class Menus
{

    public function __construct()
    {
        add_action('admin_enqueue_scripts', [$this, 'enqueueScripts']);
    }

    private function defineMenus(): array
    {
        return [
            ['Settings', __('Settings', 'wc-koin-official')],
            ['Installments', __('Installment Rules', 'wc-koin-official')]
        ];
    }

    public function initializeMenus(): void
    {
        $controllers = $this->defineMenus();
        $menus = [];

        foreach ($controllers as $key => $controller) {

            $slug     = $this->getMenuSlug($controller[0]);
            $function = WKO_PLUGIN_NAMESPACE . "\\Controllers\\Menus\\$controller[0]";
            $menu     = [
                'title'    => $controller[1],
                'slug'     => 'wc-koin-official-' . $slug,
                'function' => [new $function, 'request'],
                'position' => $key
            ];

            array_push($menus, $menu);
        }

        $gateways = [
            'wc-koin-credit' => __('Credit/Debit Card', 'wc-koin-official'),
            'wc-koin-billet' => __('Pix Parcelado', 'wc-koin-official')
        ];

        if (get_woocommerce_currency() === 'BRL') {
            $gateways['wc-koin-pix'] = __('Pix', 'wc-koin-official');
        }

        foreach ($gateways as $key => $gateway) {
            $menu = [
                'title'    => $gateway,
                'slug'     => esc_url(admin_url('admin.php?page=wc-settings&tab=checkout&section=' . strtolower($key))),
                'function' => false,
                'position' => 100
            ];

            array_push($menus, $menu);
        }

        $this->createMenus($menus);
    }

    public function getMenuSlug(string $controller): string
    {
        $split = str_split($controller);
        $slug = '';
        $count = 0;

        foreach ($split as $letter) {
            if (ctype_upper($letter)) {
                if ($count == 0) {
                    $slug .= strtolower($letter);
                } else {
                    $slug .= '_' . strtolower($letter);
                }
            } else {
                $slug .= $letter;
            }
            $count++;
        }

        return $slug;
    }

    private function createMenus(array $menus): void
    {
        add_menu_page(
            __('Koin Payments', 'wc-koin-official'),
            __('Koin Payments', 'wc-koin-official'),
            'read',
            WKO_PLUGIN_SLUG,
            false,
            'data:image/svg+xml;base64,' . base64_encode(
                '<svg width="361" height="379" viewBox="0 0 361 379" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path fill-rule="evenodd" clip-rule="evenodd" d="M-0.00390625 42.395V378.212H79.752V42.395H-0.00390625ZM209.886 168.327L361.003 378.212H260.258L163.711 248.083L209.886 168.327Z" fill="white"/>
                    <path d="M239.273 0.416504L16.7949 378.21H109.144L331.623 0.416504H239.273Z" fill="#14DD3E"/>
                </svg>'
            )
        );

        foreach ($menus as $menu) {
            add_submenu_page(
                WKO_PLUGIN_SLUG,
                $menu['title'],
                $menu['title'],
                'manage_options',
                $menu['slug'],
                $menu['function'],
                $menu['position']
            );
        }

        remove_submenu_page(WKO_PLUGIN_SLUG, WKO_PLUGIN_SLUG);
    }

    public function enqueueScripts($hook)
    {
        if (strpos($hook, 'wc-koin-official') !== false) {
            if (wp_script_is('jquery-tiptip', 'registered')) {
                wp_enqueue_script('jquery-tiptip');
            }
            if (wp_script_is('wc-enhanced-select', 'registered')) {
                wp_enqueue_script('wc-enhanced-select');
            }
            if (wp_style_is('woocommerce_admin_styles', 'registered')) {
                wp_enqueue_style('woocommerce_admin_styles');
            }
        }
    }
}
