<?php

namespace App\Http\Controllers;

use App\Http\Requests\LinkRequest;
use App\Models\VideoSetting;
use App\Utility\ActionLogCreate;
use Illuminate\Http\Request;

class TrainingVideoController extends Controller
{
    private $message;
    private $code;
    private $data;
    private $action;
    private $type;
    public function __construct()
    {
        $this->action='';
        $this->type='error';
        $this->message = 'Fail';
        $this->code = config('constant.API_FAILED_CODE');
        $this->data=[];
    }
    public function index(Request $request)
    {
        $inputData['is_welcome_video'] = false;
        $videos=(new VideoSetting())->getVideos($inputData);
        return view('training_video.list',compact('videos'));
    }
    public function list(Request $request)
    {

        $videos = (new VideoSetting())->getVideos();

        return view('training_video.video_list',compact('videos'));
    }
    public function createTrainingVideo(LinkRequest $request)
    {

            $this->action='INSERT_TRAINING_VIDEO';
            $this->message='can not save data.';

            $inputData = $request->only(['section_name','link','status','welcome_video_create']);
            $inputData['is_welcome_video'] = isset($inputData['welcome_video_create']);

            $returnData = (new VideoSetting())->insertVideo($inputData);

            if (!empty($returnData)) {
                $this->message='Successfully Saved';
                $this->code=config('constant.API_SUCCESS_CODE');
                $this->type='success';
            }
            ActionLogCreate::logDataCreate( $this->action,$request->all(),$this->message,$returnData,$this->code);
            return back()->with($this->type, $this->message);
    }
    public function editTrainingVideo(Request $request)
    {
        $this->action='EDIT_TRAINING_VIDEO';
        $this->message='can not update data.';
        $validated = $request->validate([
            'edit_section_id' => 'required|gt:0',
            'edit_link' => 'required|regex:'.config('constant.VIMEO_VIDEO_LINK_VALIDATION'),
            'edit_status' => 'required|numeric',
        ]);
        $inputData = ['section_id'=>$request["edit_section_id"],'link'=>$request["edit_link"],'status'=>$request["edit_status"]];
        $inputData['is_welcome_video'] = isset($request['welcome_video_edit']);

        $returnData = (new VideoSetting())->updateVideo($inputData);
        if (!empty($returnData)) {
            $this->message='Update Successfully';
            $this->code=config('constant.API_SUCCESS_CODE');
            $this->type='success';
        }
        ActionLogCreate::logDataCreate( $this->action,$request->all(),$this->message,$returnData,$this->code);
        return back()->with($this->type, $this->message);
    }
    public function deleteTrainingVideo(Request $request)
    {
        $this->action='DELETE_TRAINING_VIDEO';
        $this->message='can not delete data.';
        $validated = $request->validate([
            'delete_section_id' => 'required|gt:0',
        ]);
        $inputData=['section_id'=>$request['delete_section_id']];
        if($inputData['section_id']>0) {
            $returnData = (new VideoSetting())->deleteVideos($inputData);
            if (!empty($returnData)) {
                $this->message='Successfully Deleted';
                $this->code=config('constant.API_SUCCESS_CODE');
                $this->type='success';
            }
          ActionLogCreate::logDataCreate($this->action,$request->all(),$this->message,$returnData,$this->code);
        }
        return back()->with($this->type, $this->message);

    }

}
