<?php

namespace App\Http\Controllers\Owner;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Helpers\ResponseBuilder;
use App\Http\Helpers\ParamsValidator;
use App\Http\History\HistoryData;
use App\Http\History\HistoryTables;
use App\Modules\Image\Domain\ImageThumbnailData;
use App\Modules\Image\Infraestructure\BasicConfigImageStorage;
use App\Modules\Shared\Domain\ImageUtils;
use Illuminate\Support\Facades\DB;

class c_Type_Extra extends Controller
{
    private $oDomain;
    /** @var HistoryData */
    private $history;

    public function __construct()
    {
        $this->oDomain = "Type Extra";
        $this->history = new HistoryData(HistoryTables::type_extra);
    }

    public function Insert(Request $Request)
    {
        $oResponse  = array();
        $oValParams = array(
            'TypeExtra_Name'     => 'required|string|max:250',
            'TypeExtra_Abrv'     => 'required|string|max:4',
            'TypeExtra_Icon'    => 'image|mimes:' . config("var.MIME_IMG"),
            'TypeExtra_Status'   => 'required|int'
        );

        // VALIDANDO DATOS
        $oResponse = ParamsValidator::Validate_Request($Request, $oValParams, $this->oDomain);

        $thumbnail = new ImageThumbnailData(
            config("var.THUMB_W"),
            config("var.THUMB_H")
        );

        $image = (new BasicConfigImageStorage($Request))->storeImage(
            'type_extra' . DIRECTORY_SEPARATOR . 'icon',
            'TypeExtra_Icon',
            $thumbnail
        );

        //  INGRESANDO DATOS
        if ($oResponse["Response_Code"] == 200) {
            $oParam = [
                trim($Request->input("TypeExtra_Name")),
                trim($Request->input("TypeExtra_Abrv")),
                isset($image) ? $image->getUrl() : '',
                isset($image) ? $image->getThumbnail() : '',
                $Request->input("TypeExtra_Status")
            ];
            $oData = DB::select('call sp_type_extra_insert(?,?,?,?,?)', $oParam);

            //  RESPONSE
            if ($oData[0]->Response_Success == 1) {
                //region HISTORY EXEC
                $this->history->insertBasic($Request);
                //endregion
            } else {
            }

            $oResponse["Response_Status"]           = $oData[0]->Response_Status;
            $oResponse["Response_Code"]             = $oData[0]->Response_Code;
            $oResponse["Response_Domain"]           = $this->oDomain;
            $oResponse["Response_Message"]          = $oData[0]->Response_Message;
            $oResponse["Response_Data"]             = null;
            $oResponse["Response_Error_Message"]    = $oData[0]->Response_Message;
            $oResponse["Response_Error_Reason"]     = $oData[0]->Response_Reason;
        }

        return ResponseBuilder::Response($oResponse);
    }
    public function Update(Request $Request)
    {
        $oResponse  = array();
        $oValParams = array(
            'Id_TypeExtra'       => 'required|int',
            'TypeExtra_Name'     => 'required|string|max:250',
            'TypeExtra_Abrv'     => 'required|string|max:4',
            'TypeExtra_Icon'     => 'image|mimes:' . config("var.MIME_IMG"),
            'TypeExtra_Status'   => 'required|int'
        );

        // VALIDANDO DATOS
        $oResponse = ParamsValidator::Validate_Request($Request, $oValParams, $this->oDomain);

        $thumbnail = new ImageThumbnailData(
            config("var.THUMB_W"),
            config("var.THUMB_H")
        );

        $image = (new BasicConfigImageStorage($Request))->storeImage(
            'type_extra' . DIRECTORY_SEPARATOR . 'icon',
            'TypeExtra_Icon',
            $thumbnail
        );

        //  INGRESANDO DATOS
        if ($oResponse["Response_Code"] == 200) {

            //region HISTORY VARIABLES
            $this->history->obtainOld($Request->input('Id_TypeExtra'));
            //endregion

            $index = DB::select('call sp_type_extra_index(?)', [$Request->input('Id_TypeExtra')]);

            $icon = isset($image) ? $image->getUrl() : (count($index) ? $index[0]->TypeExtra_Icon : '');
            $iconThumb = isset($image) ? $image->getThumbnail() : (count($index) ? $index[0]->TypeExtra_Icon_Thumb : '');

            $oParam = [
                $Request->input("Id_TypeExtra"),
                trim($Request->input("TypeExtra_Name")),
                trim($Request->input("TypeExtra_Abrv")),
                $icon,
                $iconThumb,
                $Request->input("TypeExtra_Status")
            ];
            $oData = DB::select('call sp_type_extra_update(?,?,?,?,?,?)', $oParam);

            //  RESPONSE
            if ($oData[0]->Response_Success == 1) {
                //region HISTORY EXEC
                $this->history->updateBasic($Request->input("Id_TypeExtra"), $Request);
                //endregion
            } else {
            }

            $oResponse["Response_Status"]           = $oData[0]->Response_Status;
            $oResponse["Response_Code"]             = $oData[0]->Response_Code;
            $oResponse["Response_Domain"]           = $this->oDomain;
            $oResponse["Response_Message"]          = $oData[0]->Response_Message;
            $oResponse["Response_Data"]             = null;
            $oResponse["Response_Error_Message"]    = $oData[0]->Response_Message;
            $oResponse["Response_Error_Reason"]     = $oData[0]->Response_Reason;
        }

        return ResponseBuilder::Response($oResponse);
    }

    public function Delete(Request $Request, $Id)
    {
        $oResponse  = array();

        // VALIDANDO DATOS
        $oResponse = ParamsValidator::Validate_Id($Id, $this->oDomain);

        //  INGRESANDO DATOS
        if ($oResponse["Response_Code"] == 200) {

            //region HISTORY
            $this->history->obtainOld($Id);
            //endregion

            $oParam = array($Id);
            $oData  = DB::select('call sp_type_extra_delete(?)', $oParam);

            //  RESPONSE
            if ($oData[0]->Response_Success == 1) {
                //region HISTORY EXEC
                $this->history->deleteBasic($Id, $Request);
                //endregion
            } else {
            }

            $oResponse["Response_Status"]           = $oData[0]->Response_Status;
            $oResponse["Response_Code"]             = $oData[0]->Response_Code;
            $oResponse["Response_Domain"]           = $this->oDomain;
            $oResponse["Response_Message"]          = $oData[0]->Response_Message;
            $oResponse["Response_Data"]             = null;
            $oResponse["Response_Error_Message"]    = $oData[0]->Response_Message;
            $oResponse["Response_Error_Reason"]     = $oData[0]->Response_Reason;
        }

        return ResponseBuilder::Response($oResponse);
    }
    public function Index(Request $Request, $Id)
    {
        $oResponse  = array();

        // VALIDANDO DATOS
        $oResponse = ParamsValidator::Validate_Id($Id, $this->oDomain);

        //  INGRESANDO DATOS
        if ($oResponse["Response_Code"] == 200) {
            $oParam = array($Id);
            $oData  = array_map(
                fn($htlCamp) => self::appendPath($htlCamp),
                DB::select('call sp_type_extra_index(?)', $oParam)
            );

            $oResponse["Response_Status"]           = 200;
            $oResponse["Response_Code"]             = 200;
            $oResponse["Response_Domain"]           = $this->oDomain;
            $oResponse["Response_Message"]          = $this->oDomain . " Index";
            $oResponse["Response_Data"]             = $oData;
            $oResponse["Response_Error_Message"]    = "";
            $oResponse["Response_Error_Reason"]     = "";
        }

        return ResponseBuilder::Response($oResponse);
    }
    public function List(Request $Request, $Id)
    {
        $oResponse  = array();

        $oResponse = ParamsValidator::Validate_Action($Id, $this->oDomain);

        if ($oResponse["Response_Code"] == 200) {
            $oParam = array($Id);
            $oData  = array_map(
                fn($htlCamp) => self::appendPath($htlCamp),
                DB::select('call sp_type_extra_list(?)', $oParam)
            );

            $oResponse["Response_Status"]           = 200;
            $oResponse["Response_Code"]             = 200;
            $oResponse["Response_Domain"]           = $this->oDomain;
            $oResponse["Response_Message"]          = $this->oDomain . " list " . $Id;
            $oResponse["Response_Data"]             = $oData;
            $oResponse["Response_Error_Message"]    = "";
            $oResponse["Response_Error_Reason"]     = "";
        }

        return ResponseBuilder::Response($oResponse);
    }

    public static function appendPath($modal)
    {
        ImageUtils::appendPath($modal, 'TypeExtra_Icon');
        return ImageUtils::appendPath($modal, 'TypeExtra_Icon_Thumb');
    }

    public function Selector(Request $Request, $Id)
    {
        if (is_numeric($Id)) {
            return SELF::Index($Request, $Id);
        } else {
            return SELF::List($Request, $Id);
        }
    }
}
