<?php

namespace App\Models\Parameter;

use App\Helpers\Utils;
use App\Helpers\ParameterUtils;

class SetupType1
{
    private $data;
    private $ecodingBpa;
    private $bpa;
    private $permissionNameList = ['is_create', 'is_update', 'is_create', 'is_read', 'is_delete', 'is_create', 'is_create'];

    public function __construct($bpa, $customParaName = null)
    {
        $this->ecodingBpa = $bpa;
        $this->bpa = Utils::bpaDecoding($bpa);
        if (isset($customParaName)) {
            $this->bpa['para_name'] = $customParaName;
        }
        // dd($this->bpa);
        $this->data = Utils::getParamFile($this->bpa['para_name']);
        $this->data['General'] = array_merge($this->data['General'], ['returnUrl' => $this->bpa['page_uri'].'?bpa='.$bpa]);

        $this->converterData();
    }

    public function getData($customName = null)
    {
        if (isset($customName)) {
            return [$customName => $this->data];
        }

        return ['setupType1' => $this->data];
    }

    public function getCopyPara($SelectOptionsName, $value)
    {
        return collect($this->data[$SelectOptionsName])->where('Value', $value)->pluck('Parameter');
    }

    private function resetDisplayVarsToDefaultValues()
    {
        // 사이즈 디폴트 생성
        if (! isset($this->data['DisplayVars'])) {
            $this->data['DisplayVars']['InitLines'] = 10;
            $this->data['DisplayVars']['HeadHeight'] = '250';
            $this->data['DisplayVars']['BodyHeight'] = '400';

            foreach (['IsListFirst'] as $displayVar) {
                $this->data['DisplayVars'][$displayVar] = false;
            }
        } else {
            if (empty($this->data['DisplayVars']['InitLines']))
                $this->data['DisplayVars']['InitLines'] = 10;
            if (empty($this->data['DisplayVars']['HeadHeight']))
                $this->data['DisplayVars']['HeadHeight'] = '250';
            if (empty($this->data['DisplayVars']['BodyHeight']))
                $this->data['DisplayVars']['BodyHeight'] = '400';

            foreach (['IsListFirst'] as $displayVar) {
                if (empty($this->data['DisplayVars'][$displayVar]))
                    $this->data['DisplayVars'][$displayVar] = false;
            }
        }
    }

    private function resetFormVarsToDefaultValues()
    {
        if (! isset($this->data['DisplayVars']['IsC1Popup'])) {
            $this->data['DisplayVars']['IsC1Popup'] = '0';
        }
    }

    private function applyFormVarsSettings()
    {
        switch ($this->data['DisplayVars']['IsC1Popup']) {
            case '0':
                break;
            case '1':
                $this->data['ListVars']['Hidden']['$Radio'] = 'hidden';
                $this->data['ListVars']['Display']['$Radio'] = 'd-none';
                $this->data['ListVars']['Count']--;
                break;
            default:
                # code...
                break;
        }
    }


    private function converterData()
    {
        foreach (['HeadSelectOptions', 'BodySelectOptions'] as $varsName) {
            if (array_key_exists($varsName, $this->data)) {
                $this->createPermission($varsName);
                $this->exceptionHandling($varsName);
            }
        }

        $this->resetFormVarsToDefaultValues();

        foreach (['FormVars', 'ListVars', 'FooterVars'] as $varsName) {
            if (array_key_exists($varsName, $this->data)) {
                ParameterUtils::separateAlignAndFormat($this->data, $varsName);
                ParameterUtils::checkDisplayAndCount($this->data, $varsName);
                ParameterUtils::mappingKeys($this->data, $varsName);
            }
        }

        $this->checkSaveBtnPermission();
        $this->resetDisplayVarsToDefaultValues();

        $this->applyFormVarsSettings();
    }

    private function createPermission($SelectOptionsName)
    {
        $this->data[$SelectOptionsName] = collect($this->data[$SelectOptionsName])->map(function ($selectBtnOption, $index) {
            return array_merge($selectBtnOption, ['Permission' => $this->permissionNameList[$index] ?? 'is_create']);
        })->toArray();
    }

    private function exceptionHandling($SelectOptionsName)
    {
        $this->data[$SelectOptionsName] = collect($this->data[$SelectOptionsName])->filter(function ($selectBtnOption) {
            return $selectBtnOption['Value'] != NULL;
        })->filter(function ($selectBtnOption) {
            return $this->bpa['permission'][$selectBtnOption['Permission']] == 1;
        })->values()->toArray();
    }

    private function checkSaveBtnPermission()
    {
        if (isset($this->data['FormVars']['Title']['SaveButton']) && !$this->bpa['permission']['is_update']) {
            $this->data['FormVars']['Hidden']['SaveButton'] = 'hidden';
        }
    }
}
