<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Menu;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Factory\PriceCompareFactory;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Config\MenuConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Infrastructure\Request\Request;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareCategoryConfig;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\PriceCompare\PriceCompareAttributeConfig;
use DropshippingExportFreeVendor\WPDesk\View\Renderer\Renderer;
class SettingsMenu
{
    public const MENU_FIELD_TYPE = 'type';
    public const MENU_FIELD_TAB = 'tab';
    public const MENU_FIELD_URL = 'url';
    public const MENU_FIELD_NAME = 'name';
    public const MENU_FIELD_SUBMENU = 'submenu';
    public const MENU_CATEGORIES_TAB = 'categories';
    public const MENU_ATTRIBUTES_TAB = 'attributes';
    public const MENU_UCP_TAB = 'ucp';
    private PriceCompareFactory $price_compare_factory;
    private array $menu_array = [];
    private Renderer $renderer;
    private Request $request;
    public function __construct(PriceCompareFactory $price_compare_factory, Renderer $renderer, Request $request)
    {
        $this->price_compare_factory = $price_compare_factory;
        $this->renderer = $renderer;
        $this->request = $request;
    }
    public function output_render(): void
    {
        $menu_array = $this->get_menu_array();
        $this->renderer->output_render('Tabs', ['menu' => $menu_array, 'menu_tab' => $this->request->get_param('get.tab')->getAsString(), 'menu_type' => $this->request->get_param('get.type')->getAsString()]);
    }
    private function get_menu_array(): array
    {
        $configs = $this->price_compare_factory->get_configs();
        $this->init_menu_fields();
        foreach ($configs as $config) {
            if ($config instanceof PriceCompareCategoryConfig) {
                $this->add_categories_menu($config);
            }
            if ($config instanceof PriceCompareAttributeConfig) {
                $this->add_attributes_menu($config);
            }
        }
        return $this->menu_array;
    }
    private function init_menu_fields(): void
    {
        $this->menu_array = [self::MENU_CATEGORIES_TAB => [self::MENU_FIELD_NAME => __('Categories', 'dropshipping-export-free'), self::MENU_FIELD_TAB => self::MENU_CATEGORIES_TAB, self::MENU_FIELD_URL => $this->get_settings_page_url(), self::MENU_FIELD_SUBMENU => []], self::MENU_ATTRIBUTES_TAB => [self::MENU_FIELD_NAME => __('Category Attributes', 'dropshipping-export-free'), self::MENU_FIELD_TAB => self::MENU_ATTRIBUTES_TAB, self::MENU_FIELD_URL => $this->get_settings_page_url([self::MENU_FIELD_TAB => self::MENU_ATTRIBUTES_TAB]), self::MENU_FIELD_SUBMENU => []], self::MENU_UCP_TAB => [self::MENU_FIELD_NAME => __('Universal Commerce Protocol', 'dropshipping-export-free'), self::MENU_FIELD_TAB => self::MENU_UCP_TAB, self::MENU_FIELD_URL => $this->get_settings_page_url([self::MENU_FIELD_TAB => self::MENU_UCP_TAB]), self::MENU_FIELD_SUBMENU => []]];
    }
    private function add_categories_menu(PriceCompareConfig $config): void
    {
        $this->menu_array[self::MENU_CATEGORIES_TAB][self::MENU_FIELD_SUBMENU][$config->get_id()] = [self::MENU_FIELD_NAME => $config->get_name(), self::MENU_FIELD_TYPE => $config->get_id(), self::MENU_FIELD_URL => $this->get_settings_page_url([self::MENU_FIELD_TYPE => $config->get_id()])];
    }
    private function add_attributes_menu(PriceCompareConfig $config): void
    {
        $this->menu_array[self::MENU_ATTRIBUTES_TAB][self::MENU_FIELD_SUBMENU][$config->get_id()] = [self::MENU_FIELD_NAME => $config->get_name(), self::MENU_FIELD_TYPE => $config->get_id(), self::MENU_FIELD_URL => $this->get_settings_page_url([self::MENU_FIELD_TYPE => $config->get_id(), self::MENU_FIELD_TAB => self::MENU_ATTRIBUTES_TAB])];
    }
    private function get_settings_page_url(array $params = []): string
    {
        $args = array_merge(['page' => MenuConfig::SLUG_SETTINGS], $params);
        return add_query_arg($args, admin_url('admin.php'));
    }
}
