<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Repositories\HomePageSliderRepository;
use Illuminate\Http\Request;

class HomePageSliderController extends Controller
{
    protected $homeSliderRepi;
    public function __construct(HomePageSliderRepository $homeSliderRepository)
    {
        $this->homeSliderRepi = $homeSliderRepository;
    }


    public function index()
    {

        return $this->homeSliderRepi->indexPage();
    }

    public function createPage()
    {
        return $this->homeSliderRepi->createPage();

    }

    public function edit($id)
    {
        return $this->homeSliderRepi->editPage($id);

    }

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|max:255',
            'description' => 'required',
            'decuraton_text' => 'required',
            'btn_text' => 'required',
            'slider_video' => 'required|mimes:mp4,avi,mkv,webm,flv|max:10240'
        ]);
        try {
            $slider = $this->homeSliderRepi->create($request->all());
            if ($request->hasFile('slider_vidoe')) {
                $slider->addMediaFromRequest('slider_vidoe')->toMediaCollection('slider_vidoe');
            }
            return redirect()->route('admin.slider.list')->with('success', 'Home Slider Successfully Added');
        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());
        }

    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'title' => 'required|max:255',
            'description' => 'required',
            'decuraton_text' => 'required',
            'btn_text' => 'required',
            'slider_video' => 'nullable|mimes:mp4,avi,mkv,webm,flv|max:10240',
        ]);
        try {
            $update = $this->homeSliderRepi->update($request->all(), $id);
            if ($request->hasFile('slider_vidoe')) {
                $file = $request->file('slider_video');

                if ($file) {
                    // Confirm the file is a valid video
                    if (!in_array($file->getMimeType(), ['video/mp4', 'video/x-matroska', 'video/webm', 'video/x-flv'])) {
                        return redirect()->back()->with('error', 'The uploaded file must be a valid video.');
                    }
        
                    // Clear old media and add new video
                    $update->clearMediaCollection('slider_video');
                    $update->addMediaFromRequest('slider_video')->toMediaCollection('slider_video');
                } else {
                    return redirect()->back()->with('error', 'Only upload the video. Thank You!.');
                }
            }
            return redirect()->route('admin.slider.list')->with('success', 'Home Slider Successfully Updated');
        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());

        }

    }

    public function delete(Request $request)
    {
        $this->homeSliderRepi->delete($request->id);
        return response()->json(['success' => 'Record successfully deleted!']);
    }
}
