<?php

namespace App\Http\Controllers\Office;

use Illuminate\Http\Request;
use App\Models\SingleRowData;
use App\Services\InstallService;
use App\Http\Controllers\Controller;
use App\Services\GeneralSettingsService;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Storage;

class GeneralSettingsController extends Controller
{
    /**
     * @var $generalsettingsService
     */
    protected $generalsettingsService;

    /**
     * GeneralSettingsService constructor.
     *
     * @param GeneralSettingsRepository $generalsettingsRepository
     */
    public function __construct(GeneralSettingsService $generalsettingsService)
    {
        $this->generalsettingsService = $generalsettingsService;
    }

    public function index()
    {
        $route_active = 'general_setting';
        $logo = SingleRowData::where('field_name','logo_file')->first();
        $company_name = SingleRowData::where('field_name','company_name')->first();
        $company_address = SingleRowData::where('field_name','company_address')->first();
        $company_city = SingleRowData::where('field_name','company_city')->first();
        $company_state = SingleRowData::where('field_name','company_state')->first();
        $company_country = SingleRowData::where('field_name','company_country')->first();
        $company_zip = SingleRowData::where('field_name','company_zip')->first();
        $company_phone = SingleRowData::where('field_name','company_phone')->first();
        $company_email = SingleRowData::where('field_name','company_email')->first();
        $terms = SingleRowData::where('field_name','terms')->first();
        $customer_notes = SingleRowData::where('field_name','customer_notes')->first();
        $captcha = SingleRowData::where('field_name','captcha')->first();
        $enableCaptcha = SingleRowData::where('field_name','enableCaptcha')->first();
        $enable_twilio = SingleRowData::where('field_name','enable_twilio')->first();
        $twilio_integration = SingleRowData::where('field_name','twilio_integration')->first();
        $twilio_voice = SingleRowData::where('field_name','twilio_voice')->first();
        $enable_twilio_voice = SingleRowData::where('field_name','enable_twilio_voice')->first();
        return view('crm.office.general_setting', compact([
            'route_active', 
            'logo',
            'company_name',
            'company_address',
            'company_city',
            'company_state',
            'company_country',
            'company_zip',
            'company_phone',
            'company_email',
            'terms',
            'customer_notes',
            'captcha',
            'enableCaptcha',
            'twilio_integration',
            'enable_twilio',
            'twilio_voice',
            'enable_twilio_voice'
        ]));
    }

    // Company Logo 
    public function store(Request $request)
    {
        if($request->has('logo_file')){
        // if you want to delete the image from the directory
           $extension = ".".$request->logo_file->getClientOriginalExtension();
           $name = basename($request->logo_file->getClientOriginalName(), $extension).time();
           $name = $name.$extension;
           $path = $request->logo_file->storeAs('adminfiles',$name,'public');
         }
         if($extension == '.png' || $extension == '.jpg' || $extension == '.jpeg' || $extension == '.gif' || $extension == '.PNG' || $extension == '.JPG' || $extension == '.JPEG' || $extension == '.GIF') {
             $extension = 'image';
         }
         $logo = SingleRowData::create([
             'field_name'=>'logo_file',
             'field_type'=>'file',
             'field_value'=>$name,
        ]);

        if($logo->save()){
            $notification = array(
                'message' => __('File uploaded successfully!'),
                'alert-type' => 'success'
            );
            return back()->with($notification);
        }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return back()->with($notification);
        }
    }

    // Terms and Conditions Create
    public function terms(Request $request)
    {
        $validated = $request->validate([
            'terms'=>'required',
        ]);
        if($validated){
            $terms = SingleRowData::create([
                'field_name'=>'terms',
                'field_type'=>'text',
                'field_value'=>$request->terms,
           ]);
           if($terms){
               $notification = array(
                   'message' => __('Terms added successfully!'),
                   'alert-type' => 'success'
               );
               return back()->with($notification);
           }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return back()->with($notification); 
           }
        }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return back()->with($notification);            
        }
    }

    // Terms and Conditions Update
    public function termsUpdate(SingleRowData $terms, Request $request)
    {
        $validated = $request->validate([
            'terms'=>'required',
        ]);
        if($validated){
            $terms->field_value = $request->terms;
            if($terms->save()){
                $notification = array(
                    'message' => __('Terms updated successfully!'),
                    'alert-type' => 'success'
                );
                return back()->with($notification);
            }else{
                $notification = array(
                    'message' => __('Please try again!'),
                    'alert-type' => 'error'
                );
                return back()->with($notification); 
            }
        }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return back()->with($notification);            
        }
    }

    // Google captcha Create
    public function captcha(Request $request){
        return $this->generalsettingsService->captcha($request);
    }

    // Google captcha Update
    public function captchaUpdate(SingleRowData $captcha, Request $request){
        return $this->generalsettingsService->captchaUpdate($captcha, $request);
    }

    // Google enable/disable Captcha store
    public function enableCaptcha(Request $request){
        return $this->generalsettingsService->enableCaptcha($request);
    }

    // Google enable/disable captcha update
    public function enableCaptchaUpdate(SingleRowData $enableCaptcha, Request $request){
        return $this->generalsettingsService->enableCaptchaUpdate($enableCaptcha, $request);
    }

    // Customer Notes Create
    public function customer_notes(Request $request)
    {
        $validated = $request->validate([
            'customer_notes'=>'required',
        ]);
        if($validated){
            $customer_notes = SingleRowData::create([
                'field_name'=>'customer_notes',
                'field_type'=>'text',
                'field_value'=>$request->customer_notes,
           ]);
           if($customer_notes){
               $notification = array(
                   'message' => __('Default customer notes added successfully!'),
                   'alert-type' => 'success'
               );
               return back()->with($notification);
           }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return back()->with($notification); 
           }
        }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return back()->with($notification);            
        }
    }

    // Customer Notes Update
    public function customer_notes_update(SingleRowData $customer_notes, Request $request)
    {
        $validated = $request->validate([
            'customer_notes'=>'required',
        ]);
        if($validated){
            $customer_notes->field_value = $request->customer_notes;
            if($customer_notes->save()){
                $notification = array(
                    'message' => __('Default customer notes updated successfully!'),
                    'alert-type' => 'success'
                );
                return back()->with($notification);
            }else{
                $notification = array(
                    'message' => __('Please try again!'),
                    'alert-type' => 'error'
                );
                return back()->with($notification); 
            }
        }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return back()->with($notification);            
        }
    }

    // Company Address Store
    public function storeDetails(Request $request)
    {
        $validated = $request->validate([
            'company_name'=>'required'
        ]);
        $company_name = SingleRowData::create([
             'field_name'=>'company_name',
             'field_type'=>'text',
             'field_value'=>$request->company_name,
        ]);
        SingleRowData::create([
            'field_name'=>'company_address',
            'field_type'=>'text',
            'field_value'=>$request->company_address,
       ]);
       SingleRowData::create([
        'field_name'=>'company_city',
        'field_type'=>'text',
        'field_value'=>$request->company_city,
        ]);
        SingleRowData::create([
            'field_name'=>'company_state',
            'field_type'=>'text',
            'field_value'=>$request->company_state,
        ]);
        SingleRowData::create([
            'field_name'=>'company_country',
            'field_type'=>'text',
            'field_value'=>$request->company_country,
        ]);
        SingleRowData::create([
            'field_name'=>'company_zip',
            'field_type'=>'text',
            'field_value'=>$request->company_zip,
        ]);
        SingleRowData::create([
            'field_name'=>'company_phone',
            'field_type'=>'text',
            'field_value'=>$request->company_phone,
        ]);
        SingleRowData::create([
            'field_name'=>'company_email',
            'field_type'=>'text',
            'field_value'=>$request->company_email,
        ]);

        if($validated){
            $notification = array(
                'message' => __('Company details saved successfully!'),
                'alert-type' => 'success'
            );
            return back()->with($notification);
        }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return back()->with($notification);
        }
    }

    // Company Address Update
    public function updateDetails(Request $request, $company_name)
    {
        $validated = $request->validate([
            'company_name'=>'required'
        ]);

        $company_name = SingleRowData::where('field_name','company_name')->first();
        $company_name->field_value = $request->company_name;
        $company_name->save();

        $company_address = SingleRowData::where('field_name','company_address')->first();
        $company_address->field_value = $request->company_address;
        $company_address->save();
        
        $company_city = SingleRowData::where('field_name','company_city')->first();
        $company_city->field_value = $request->company_city;
        $company_city->save();
        
        $company_state = SingleRowData::where('field_name','company_state')->first();
        $company_state->field_value = $request->company_state;
        $company_state->save();
        
        $company_country = SingleRowData::where('field_name','company_country')->first();
        $company_country->field_value = $request->company_country;
        $company_country->save();
        
        $company_zip = SingleRowData::where('field_name','company_zip')->first();
        $company_zip->field_value = $request->company_zip;
        $company_zip->save();
        
        $company_phone = SingleRowData::where('field_name','company_phone')->first();
        $company_phone->field_value = $request->company_phone;
        $company_phone->save();
        
        $company_email = SingleRowData::where('field_name','company_email')->first();
        $company_email->field_value = $request->company_email;
        $company_email->save();

        if($validated){
            $notification = array(
                'message' => __('Company details updated successfully!'),
                'alert-type' => 'success'
            );
            return back()->with($notification);
        }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return back()->with($notification);
        }
    }    

    public function update(Request $request,  $logo)
    {
        if($request->has('logo_file')){
            // if you want to delete the image from the directory
           Storage::delete('adminfiles'.$request->logo_file);
           $extension = ".".$request->logo_file->getClientOriginalExtension();
           $name = basename($request->logo_file->getClientOriginalName(), $extension).time();
           $name = $name.$extension;
           $path = $request->logo_file->storeAs('adminfiles',$name,'public');
         }
         if($extension == '.png' || $extension == '.jpg' || $extension == '.jpeg' || $extension == '.gif' || $extension == '.PNG' || $extension == '.JPG' || $extension == '.JPEG' || $extension == '.GIF') {
             $extension = 'image';
         }
        $logo = SingleRowData::find($logo);

        $logo->field_value = $name;
        
        if($logo->save()){
            $notification = array(
                'message' => __('File uploaded successfully!'),
                'alert-type' => 'success'
            );
            return back()->with($notification);
        }else{
            $notification = array(
                'message' => __('Please try again!'),
                'alert-type' => 'error'
            );
            return back()->with($notification);
        }
    }

    public function verify_licence(Request $request){
        if(!$request->purchase_code){
            $notification = array(
                'message' => "Please enter your purchase code!",
                'alert-type' => 'error'
            );
            return back()->with($notification)->withInput();
        }
        $purchase_code = trim($request->purchase_code);
        
        $response = InstallService::callAPI($purchase_code);

        if ($response['status_code'] !== 200) {
            session()->put('message', 'Please enter your valid purchase code!');
            $message = 'Please enter your purchase code!';
            session()->put('alert-type', 'error');
            return back()->with('message');
        }else{
            $licence_insert = SingleRowData::where([
                'field_name'=>'licence_number',
                'field_value'=>$purchase_code,
            ])->first();
            if(!$licence_insert){
                $licence_insert = SingleRowData::create([
                    'field_name'=>'licence_number',
                    'field_type'=>'text',
                    'field_value'=>$purchase_code,
                ]);
            }
            if($licence_insert){
                $notification = array(
                    'message' => "Your purchase code is valid!",
                    'alert-type' => 'success'
                );
                session()->put('licence_verified', true);
                $api_url = "https://www.devpremier.com/devpremier_crm_latest_version.php";
                $response = Http::post($api_url);
                if(json_decode($response->getBody()) != null){
                    $latest_version = json_decode($response->getBody())->latest_version;
                }
                session()->put('latest_version', $latest_version);

                return back()->with($notification)->withInput();
            }else{
                $notification = array(
                    'message' => "Please try again!",
                    'alert-type' => 'error'
                );
                return back()->with($notification)->withInput();               
            }
        }        
    }

    public function crm_upgrade(){
        Artisan::call('optimize:clear');
        Artisan::call('migrate');
        Artisan::call('config:cache');

        $notification = array(
            'message' => "You application version is updated!",
            'alert-type' => 'success'
        );
        return back()->with($notification);
    }

    public function twilio_integration(Request $request){
        return $this->generalsettingsService->twilio_integration($request);
    }

    public function twilio_integration_update(SingleRowData $twilio_integration, Request $request){
        return $this->generalsettingsService->twilio_integration_update($twilio_integration, $request);
    }

    public function send_test_sms(Request $request){
        return $this->generalsettingsService->send_test_sms($request);
    } 

    public function twilio_call(Request $request){
        return $this->generalsettingsService->twilio_call($request);
    } 

    // Enable_twilio Create
    public function enable_twilio(Request $request){
        return $this->generalsettingsService->enable_twilio($request);
    }

    // Enable_twilio Update
    public function enable_twilio_update(SingleRowData $enable_twilio, Request $request){
        return $this->generalsettingsService->enable_twilio_update($enable_twilio, $request);
    } 

    // Twilio Voice Setup Keys Store
    public function twilio_voice(Request $request){
        return $this->generalsettingsService->twilio_voice($request);
    }

    // Twilio Voice Setup Keys Update
    public function twilio_voice_update(SingleRowData $twilio_voice, Request $request){
        return $this->generalsettingsService->twilio_voice_update($twilio_voice, $request);
    }

    // Twilio WhatsApp Setup Keys Store
    public function twilio_whatsapp(Request $request){
        return $this->generalsettingsService->twilio_whatsapp($request);
    }

    // Twilio WhatsApp Setup Keys Update
    public function twilio_whatsapp_update(SingleRowData $twilio_whatsapp, Request $request){
        return $this->generalsettingsService->twilio_whatsapp_update($twilio_whatsapp, $request);
    }

    // Twilio enable/disable Captcha store
    public function enable_twilio_voice(Request $request){
        return $this->generalsettingsService->enable_twilio_voice($request);
    }

    // Twilio enable/disable captcha update
    public function enable_twilio_voice_update(SingleRowData $enable_twilio_voice, Request $request){
        return $this->generalsettingsService->enable_twilio_voice_update($enable_twilio_voice, $request);
    }

}
