<?php

namespace App\Method\Decentro\Response;

use App\Method\Decentro\DecentroMethods;
use App\Method\Decentro\Helpers\DecentroHelper;
use App\DecentroCkyc;
use App\APIWrapper\Wrapper;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use App\Http\Controllers\Admin\ConsumerDocument;
use Illuminate\Support\Facades\Input;
use Illuminate\Validation\Rules\In;

class Response implements DecentroHelper
{
    public $errorBag;
    public $response;


    public function validate($res){
        return $res;       
    }
    
    public function resolveResponse($func, $res, $request = null){
        if(method_exists($this, $func)){
            return $this->$func($res);
        }
        return array();
    }

    public function ckycSearch($res){
        if ((isset($res->status) && $res->status != "SUCCESS") || !isset($res->status) || (isset($res->data->kycStatus) && $res->data->kycStatus == "FAILURE")) {
            $this->errorBag['Error'] = $res->message ?? $res->data->message;
            $this->response = $res;
            return $this;
        }
        $id = Input::get('id');
        $count = DecentroCkyc::where('reference_id', $id)->count();
        if($count == 0){
            DecentroCkyc::insert([
                'ckyc_id' =>$res->data->kycResult->ckycId,
                'result' =>$res->data->kycStatus,
                'updatedDate' =>   $res->data->kycResult->updatedDate,
                'created_at' => Carbon::now()->toDateTimeString(),
                'updated_at' =>  Carbon::now()->toDateTimeString(),
                'Pan' => Input::get('pan'),
                'DOB' => Input::get('dob'),
                'reference_id' => $id        
            ]);
            $filePath = $id."/".$id."_response_decentro_ckyc_search.json";
            $s3 = Storage::disk('s3');
            $s3->put($filePath, json_encode($res));
        }
        $this->response = $res;
        return $this;


    }

    public function ckycDownload($res){
        if ((isset($res->status) && $res->status != "SUCCESS") || !isset($res->status) || (isset($res->data->kycStatus) && $res->data->kycStatus == "FAILURE")) {
            $this->errorBag['Error'] = $res->message ?? $res->data->message;
            $this->response = $res;
            return $this;
        }
        $id = Input::get('id');
        
        $count = DecentroCkyc::where('reference_id', $id)->first(['updatedDate']);
        if(!empty($count)){
            DecentroCkyc::where('reference_id', $id)->update(['responseDownload'=>json_encode(array('personalDetails' =>$res->data->kycResult->personalIdentifiableData->personalDetails))]);
            $filePath = $id.'/'.$id."_response_decentro_ckyc_download.json";
            $s3 = Storage::disk('s3');
            $s3->put($filePath, json_encode($res));
            /*$date1 = Carbon::parse($count->updatedDate);
            $today = Carbon::now()->endOfDay()->toDateTimeString();
            $date2 = Carbon::parse($today);
            $diffDays = $date1->diffInDays($date2);
            if($diffDays <= 365){
                $consumerDocument = new ConsumerDocument(new Request());
                foreach($res->data->kycResult->personalIdentifiableData->imageDetails->image as $val){
                    if($val->imageCode == "Photograph") {
                        $consumerDocument->uploadToS3('CUSTPHOTO_'.$id.'_1', $id, base64_decode($val->imageData, '.'.strtolower($val->imageType)));
                    }
                    if($val->imageCode == "PAN") {
                        $consumerDocument->uploadToS3('pan_card_'.$id.'_1', $id, base64_decode($val->imageData, '.'.strtolower($val->imageType)));
                    }
                    if($val->imageCode == "Signature") {
                        $consumerDocument->uploadToS3('signature_'.$id.'_1', $id, base64_decode($val->imageData, '.'.strtolower($val->imageType)));
                    }
                    if($val->imageCode == "Voters Identity Card") {
                        $consumerDocument->uploadToS3('votar_id_front_'.$id.'_1', $id, base64_decode($val->imageData, '.'.strtolower($val->imageType)));
                    }
                    if($val->imageCode == "Proof of Possession of Aadhaar") {
                        $consumerDocument->uploadToS3('aadhar_card_'.$id.'_1', $id, base64_decode($val->imageData, '.'.strtolower($val->imageType)));
                    }
                }
            }*/

        }
        $this->response = $res;
        return $this;
        
    }
}