<?php

namespace Hura8\Components\ComboSet\Controller;

use Hura8\Components\ComboSet\Model\ComboSetLanguageModel;
use Hura8\Components\ComboSet\Model\ComboSetModel;
use Hura8\System\Controller\aEntityBaseController;

class bComboSetController extends aEntityBaseController
{
    /* @var ComboSetModel $objComboSetModel */
    protected  $objComboSetModel;

    /* @var ComboSetLanguageModel $objComboSetLanguageModel */
    protected  $objComboSetLanguageModel;

    public function __construct()
    {
        $this->objComboSetModel = new ComboSetModel();

        if(!$this->isDefaultLanguage()) {
            $this->objComboSetLanguageModel = new ComboSetLanguageModel();
            //$this->objVideoLanguageModel->createTableLang();
            parent::__construct($this->objComboSetModel, $this->objComboSetLanguageModel);

        }else{
            parent::__construct($this->objComboSetModel);
        }
    }


    public function getAllSetIdsForAProduct($product_id)
    {
        return $this->objComboSetModel->getAllSetIdsForAProduct($product_id);
    }


    public function getTotalProductUseSet($set_id)
    {
        return $this->objComboSetModel->getTotalProductUseSet($set_id);
    }


    public function getListProductUseSet($set_id, $numPerPage)
    {
        return $this->objComboSetModel->getListProductUseSet($set_id, $numPerPage);
    }





    public function getProductListInfoInConfig(array $category) {
        $product_list_ids = [];
        foreach ($category as $index => $_category_info) {
            foreach ($_category_info['suggest_list'] as $_proindex => $_pro_info) {
                $product_list_ids[] = $_pro_info['real_id'];
            }
        }

        return array_unique($product_list_ids);
    }


    public function buildConfig( $category, $product) {

        $group_category = [];

        foreach ($category as $category_index => $_category_info) {
            $category_product = [];
            foreach ($product[$category_index] as $product_index => $_product_info) {
                //$_product_info['price'] = clean_price($_product_info['price']);
                $category_product[] = $_product_info;
            }

            $group_category[] = [
                "title"         => $_category_info['title'],
                //"type"          => "category",
                //"real_id"       => $_category_info['real_id'],
                //"select_type"   => $_category_info['select_type'],//checkbox|radio
                "suggest_list"  => $category_product,
            ];
        }

        return $group_category;
    }


    public function decomposeConfig($config) {
        $tab = [];
        $group = [];
        $category = [];
        $product = [];

        $group_index = 0;
        $category_index = 0;
        $product_index = 0;

        foreach ($config as $tab_index => $tab_info) {
            //construct tab
            $tab[$tab_index] = [
                'title' => $tab_info['title'],
            ];

            //construct group
            foreach ($tab_info['child'] as $child_group) {
                $group_index += 1;

                $group[$tab_index][$group_index] = [
                    'title' => $child_group['title'],
                ];

                //construct category
                foreach ($child_group['child'] as $child_category) {
                    $category_index += 1;

                    $category[$group_index][$category_index] = [
                        'title'         => $child_category['title'],
                        'real_id'       => $child_category['real_id'],
                        'select_type'   => $child_category['select_type'],
                    ];

                    //construct product
                    foreach ($child_category['suggest_list'] as $child_product) {
                        $product_index += 1;

                        $product[$category_index][$product_index] = [
                            'title'         => $child_product['title'],
                            'real_id'       => $child_product['real_id'],
                            'is_default'   => $child_product['is_default'],
                        ];
                    }
                }
            }
        }

        return [
            "tab"       => $tab,
            "group"     => $group,
            'category'  => $category,
            'product'   => $product,
        ];
    }




}
