<?php

namespace App\Http\Livewire\Public\Tools;

use Livewire\Component;
use App\Models\Admin\History;
use Illuminate\Support\Facades\Http;
use App\Classes\CreditCardValidatorClass;
use DateTime;
use GeoIp2\Database\Reader;
use GeoIp2\Exception\AddressNotFoundException;
use App\Rules\VerifyRecaptcha;
use App\Models\Admin\General;

class CreditCardValidator extends Component
{

    public $type = 'visa';
    public $code;
    public $data = [];
    public $recaptcha;
    public $generalSettings;

    public function mount()
    {
        $this->generalSettings = General::first();
    }
    
    public function render()
    {
         return view('livewire.public.tools.credit-card-validator');
    }

    /**
     * -------------------------------------------------------------------------------
     *  onCreditCardValidator
     * -------------------------------------------------------------------------------
    **/
    public function onCreditCardValidator(){

        $validationRules = [
            'code' => 'required'
        ];

        if ( $this->generalSettings->captcha_status && ($this->generalSettings->captcha_for_registered || !auth()->check()) ) {
            $validationRules['recaptcha'] = ['required', new VerifyRecaptcha];
        }

        $this->validate($validationRules);

        try {

                $output = new CreditCardValidatorClass();

                switch ($this->type) {
                    case 'amex':
                            if ( $output->Validator( $this->code )['type'] == 'amex') {

                                session()->flash('status', 'success');
                                session()->flash('message', __( 'Credit card number is Valid' ));
                                return;

                            } else {

                                session()->flash('status', 'error');
                                session()->flash('message', __( 'Credit card number is Invalid.' ));
                                return;

                            }
                        break;

                    case 'diners':
                            if ( $output->Validator( $this->code )['type'] == 'diners') {

                                session()->flash('status', 'success');
                                session()->flash('message', __( 'Credit card number is Valid' ));
                                return;

                            } else {

                                session()->flash('status', 'error');
                                session()->flash('message', __( 'Credit card number is Invalid.' ));
                                return;

                            }
                        break;

                    case 'discover':
                            if ( $output->Validator( $this->code )['type'] == 'discover') {

                                session()->flash('status', 'success');
                                session()->flash('message', __( 'Credit card number is Valid' ));
                                return;

                            } else {

                                session()->flash('status', 'error');
                                session()->flash('message', __( 'Credit card number is Invalid.' ));
                                return;

                            }
                        break;

                    case 'jcb':
                            if ( $output->Validator( $this->code )['type'] == 'jcb') {

                                session()->flash('status', 'success');
                                session()->flash('message', __( 'Credit card number is Valid' ));
                                return;

                            } else {

                                session()->flash('status', 'error');
                                session()->flash('message', __( 'Credit card number is Invalid.' ));
                                return;

                            }
                        break;

                    case 'mastercard':
                            if ( $output->Validator( $this->code )['type'] == 'mastercard') {

                                session()->flash('status', 'success');
                                session()->flash('message', __( 'Credit card number is Valid' ));
                                return;

                            } else {

                                session()->flash('status', 'error');
                                session()->flash('message', __( 'Credit card number is Invalid.' ));
                                return;

                            }
                        break;

                    default:
                            if ( $output->Validator( $this->code )['type'] == 'visa') {

                                session()->flash('status', 'success');
                                session()->flash('message', __( 'Credit card number is Valid' ));
                                return;

                            } else {

                                session()->flash('status', 'error');
                                session()->flash('message', __( 'Credit card number is Invalid.' ));
                                return;

                            }
                        break;
                }

                $this->dispatchBrowserEvent('resetReCaptcha');

        } catch (\Exception $e) {

            $this->addError('error', __($e->getMessage()));
        }

        //Save History
        $history             = new History;
        $history->tool_name  = 'Credit Card Validator';
        $history->client_ip  = request()->ip();

        require app_path('Classes/geoip2.phar');

        $reader = new Reader( app_path('Classes/GeoLite2-City.mmdb') );

        try {

            $record           = $reader->city( request()->ip() );

            $history->flag    = strtolower( $record->country->isoCode );
            
            $history->country = strip_tags( $record->country->name );

        } catch (AddressNotFoundException $e) {

        }

        $history->created_at = new DateTime();
        $history->save();
    }

    /**
     * -------------------------------------------------------------------------------
     *  onSample
     * -------------------------------------------------------------------------------
    **/
    public function onSample()
    {
        $this->type = 'visa';
        $this->code = '4916592289993918';
    }

    /**
     * -------------------------------------------------------------------------------
     *  onReset
     * -------------------------------------------------------------------------------
    **/
    public function onReset()
    {
        $this->type = 'visa';
        $this->code = null;
        $this->data = [];
    }
}
