<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Brian2694\Toastr\Facades\Toastr;
use App\DataTables\WebsiteFaqDataTable;
use App\Repositories\LanguageRepository;
use App\Repositories\WebsiteFaqRepository;

class WebsiteFaqController extends Controller
{
    protected $faqRepository;

    public function __construct(WebsiteFaqRepository $faqRepository)
    {
        $this->faqRepository = $faqRepository;
    }

    public function index(WebsiteFaqDataTable $dataTable)
    {
        return $dataTable->render('backend.admin.website.faq.index');
    }

    public function create(Request $request): \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Contracts\Foundation\Application
    {
        $lang        = $request->lang ?? app()->getLocale();
        $data        = [
            'lang'                 => $lang,
        ];

        return view('backend.admin.website.faq.create', $data);
    }

    public function store(Request $request): \Illuminate\Http\JsonResponse
    {
        if (isDemoMode()) {
            $data = [
                'status' => 'danger',
                'error'  => __('this_function_is_disabled_in_demo_server'),
                'title'  => 'error',
            ];

            return response()->json($data);
        }

        $request->validate([
            'question' => 'required',
            'answer'   => 'required',
            'ordering' => 'required',
        ]);

        DB::beginTransaction();
        try {
            $this->faqRepository->store($request);
            Toastr::success(__('create_successful'));

            DB::commit();

            return response()->json([
                'success' => __('create_successful'),
                'route'   => route('faqs.index'),
            ]);
        }catch (\Exception $e) {
            DB::rollBack();

            return response()->json(['status' => false,'error' => '']);
        }
    }

    public function edit($id, Request $request, LanguageRepository $language): \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Contracts\Foundation\Application
    {
        try {
            $faq         = $this->faqRepository->find($id);
            $lang        = $request->lang ?? app()->getLocale();
            $data        = [
                'lang'            => $lang,
                'faq_language'    => $this->faqRepository->getByLang($id, $lang),
                'faq'             => $faq,
            ];

            return view('backend.admin.website.faq.edit', $data);
        }catch (\Exception $e) {
            Toastr::error('');

            return back();
        }
    }

    public function update(Request $request, $id): \Illuminate\Http\JsonResponse
    {
        if (isDemoMode()) {
            $data = [
                'status' => 'danger',
                'error'  => __('this_function_is_disabled_in_demo_server'),
                'title'  => 'error',
            ];

            return response()->json($data);
        }
        $request->validate([
            'question' => 'required',
            'answer'   => 'required',
            'ordering' => 'required',
        ]);
        DB::beginTransaction();
        try {
            $this->faqRepository->update($request, $id);
            Toastr::success(__('update_successful'));
            DB::commit();

            return response()->json([
                'success' => __('update_successful'),
                'route'   => route('faqs.index'),
            ]);
        }catch (\Exception $e) {
            DB::rollBack();

            return response()->json(['status' => false,'error' => '']);
        }
    }

    public function statusChange(Request $request): \Illuminate\Http\JsonResponse
    {
        if (isDemoMode()) {
            $data = [
                'status'  => 'danger',
                'message' => __('this_function_is_disabled_in_demo_server'),
                'title'   => 'error',
            ];

            return response()->json($data);
        }
        try {
            $this->faqRepository->status($request->all());
            $data = [
                'status'  => 200,
                'message' => __('update_successful'),
                'title'   => 'success',
            ];

            return response()->json($data);
        }catch (\Exception $e) {
            $data = [
                'status'  => 400,
                'message' =>  __('something_went_wrong_please_try_again'),
                'title'   => 'error',
            ];

            return response()->json($data);
        }
    }

    public function destroy($id): \Illuminate\Http\JsonResponse
    {
        if (isDemoMode()) {
            $data = [
                'status'  => 'danger',
                'message' => __('this_function_is_disabled_in_demo_server'),
                'title'   => 'error',
            ];

            return response()->json($data);
        }
        try {
            $this->faqRepository->destroy($id);
            Toastr::success(__('delete_successful'));
            $data = [
                'status'  => 'success',
                'message' => __('delete_successful'),
                'title'   => __('success'),
            ];

            return response()->json($data);
        }catch (\Exception $e) {
            $data = [
                'status'  => 'danger',
                'message' =>  __('something_went_wrong_please_try_again'),
                'title'   => __('error'),
            ];

            return response()->json($data);
        }
    }
}