<?php

namespace App\Repositories;

use Illuminate\Http\Request;
use App\Models\SingleRowData;

class GoogleRepository
{
    /**
     * @var singlerowdata
     */
    protected $singlerowdata;

    /**
     * @GoogleRepository Constructory
     */
    public function __construct(SingleRowData $singlerowdata)
    {
        $this->singlerowdata = $singlerowdata;
    }

    public function captcha(Request $request){
        $validated = $request->validate([
            'captcha'=>'required',
        ]);
        if($validated){
            $captcha = SingleRowData::create([
                'field_name'=>'captcha',
                'field_type'=>'text',
                'field_value'=>$request->captcha,
            ]);
            if($captcha){
                $notification = array(
                    'message' => __('Captcha added successfully!'),
                    'alert-type' => 'success'
                );
                return redirect()->to(url()->previous() . '#google-integration-id')->with($notification);
            }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return redirect()->to(url()->previous() . '#google-integration-id')->with($notification); 
            }
        }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return redirect()->to(url()->previous() . '#google-integration-id')->with($notification);            
        }
    }

    public function captchaUpdate(SingleRowData $captcha, Request $request){
        $validated = $request->validate([
            'captcha'=>'required',
        ]);
        if($validated){
            $captcha->field_value = $request->captcha;
            if($captcha->save()){
                $notification = array(
                    'message' => __('Captcha updated successfully!'),
                    'alert-type' => 'success'
                );
                return redirect()->to(url()->previous() . '#google-integration-id')->with($notification);
            }else{
                $notification = array(
                    'message' => __('Please try again!'),
                    'alert-type' => 'error'
                );
                return redirect()->to(url()->previous() . '#google-integration-id')->with($notification); 
            }
        }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return redirect()->to(url()->previous() . '#google-integration-id')->with($notification);            
        }
    }

    public function enableCaptcha(Request $request){
        $validated = $request->validate([
            'enableCaptcha'=>'required',
        ]);
        if($validated){
            $enableCaptcha = SingleRowData::create([
                'field_name'=>'enableCaptcha',
                'field_type'=>'text',
                'field_value'=>$request->enableCaptcha,
            ]);
            if($enableCaptcha){
                $notification = array(
                    'message' => __('Captcha updated successfully!'),
                    'alert-type' => 'success'
                );
                return redirect()->to(url()->previous() . '#google-integration-id')->with($notification);
            }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return redirect()->to(url()->previous() . '#google-integration-id')->with($notification); 
            }
        }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return redirect()->to(url()->previous() . '#google-integration-id')->with($notification);            
        }
    }

    public function enableCaptchaUpdate(SingleRowData $enableCaptcha, Request $request){
        $enableCaptcha->field_value = ($request->disablecaptcha == 'true') ? 'no' : 'yes';
        if($enableCaptcha->save()){
            $notification = array(
                'message' => __('Captcha updated successfully!'),
                'alert-type' => 'success'
            );
            return redirect()->to(url()->previous() . '#google-integration-id')->with($notification);
        }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return redirect()->to(url()->previous() . '#google-integration-id')->with($notification); 
        }
    }  
}

?>