<?php

namespace App\Method\KMGP2P\Response;

use App\Method\KMGP2P\Helpers\KMGP2PHelper;
use App\KMGP2PBorrowerReg;
use App\KMGP2PDocument;
use Illuminate\Support\Carbon;
use App\Slack;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class Response implements KMGP2PHelper
{
    public $errorBag;
    public $response;
    public $responseData;

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

    public function borrowerRegistration($res){
        (new Slack(new Request(['title'=>"KMGP2P Borrower registration Response", 'fields'=>['Message' => json_encode($res)]])))->send();
        if(isset($res->status) && $res->status != 'OK'){
            $this->errorBag['Error'] = $res->message;
            $this->response = $res;
            return $this;
        }
        if(isset($res->error->description)){
            $this->errorBag['Error'] = $res->error->description;
            $this->response = $res;
            return $this;
        }
        if(!isset($res->status)){
            $this->errorBag['Error'] = 'Getting empty response';
            $this->response = $res;
            return $this;
        }

        try{
            $KMGP2P = KMGP2PBorrowerReg::where('application_id', Input::get('id'))->first(['id']);
            if (!isset($KMGP2P->id)){
                $result = 'failure';
                if($res->status == 'OK'){
                    $result = 'success';
                }
                KMGP2PBorrowerReg::insert([
                    'application_id' => Input::get('id'),
                    'result' => $result,
                    'kmg_user_id' => $res->data->kmUserId,
                    'kmg_loan_id' => $res->data->kmLoanId,
                    'user_id' =>Input::get('user_id'),
                    'created_at' =>Carbon::now()->toDateTimeString(),
                    'updated_at' =>Carbon::now()->toDateTimeString()
                ]);
            }
        }catch (\Exception $e){
            (new Slack(new Request(['title'=>'KMGP2P borrower registration issue','fields'=>['error'=>$e->getMessage(),'line'=>$e->getLine(),'trace'=>$e->getTraceAsString()]])))->send();
        }
        return $this->response = $res;
    }

    public function uploadDocument($res){
        (new Slack(new Request(['title'=>"KMGP2P Borrower document upload Response", 'fields'=>['Message' => json_encode($res)]])))->send();
        if(isset($res->status) && $res->status != 'OK'){
            $this->errorBag['Error'] = $res->message;
            $this->response = $res;
            return $this;
        }
        if(isset($res->error->description)){
            $this->errorBag['Error'] = $res->error->description;
            $this->response = $res;
            return $this;
        }
        if(!isset($res->status)){
            $this->errorBag['Error'] = 'Getting empty response';
            $this->response = $res;
            return $this;
        }

        try{
            $result = 'failure';
            if($res->status == 'OK'){
                $result = 'success';
            }
            $KMGP2PDocuments = new KMGP2PDocument();
            $kmgDoc = KMGP2PDocument::where('application_id',Input::get('id'))->where('file_id',Input::get('file_id'))->count();
            if($kmgDoc == 0){
                $KMGP2PDocuments->fill([
                    'application_id' => Input::get('id'),
                    'file_id' => Input::get('file_id'),
                    'response' => json_encode($res),
                    'status' => $result,
                    'user_id' => Input::get('user_id'),
                    'created_at' =>Carbon::now()->toDateTimeString(),
                    'updated_at' =>Carbon::now()->toDateTimeString()
                ]);
                $KMGP2PDocuments->save();
            }else{
                $update = [
                    'response' => json_encode($res),
                    'status' => $result,
                    'user_id' => Input::get('user_id'),
                    'updated_at' =>Carbon::now()->toDateTimeString()
                ];
                KMGP2PDocument::where('file_id',Input::get('file_id'))->update($update);
            }
        }catch (\Exception $e){
            (new Slack(new Request(['title'=>'KMGP2P upload document issue','fields'=>['error'=>$e->getMessage(),'line'=>$e->getLine(),'trace'=>$e->getTraceAsString()]])))->send();
        }
        return $this->response = $res;
    }

    public function saveEMI($res){
        (new Slack(new Request(['title'=>"KMGP2P Borrower save emi calculation Response", 'fields'=>['Message' => json_encode($res)]])))->send();
        if(isset($res->status) && $res->status != 'OK'){
            $this->errorBag['Error'] = $res->message;
            $this->response = $res;
            return $this;
        }
        if(isset($res->error->description)){
            $this->errorBag['Error'] = $res->error->description;
            $this->response = $res;
            return $this;
        }
        if(!isset($res->status)){
            $this->errorBag['Error'] = 'Getting empty response';
            $this->response = $res;
            return $this;
        }

        return $this->response = $res;
    }

    public function updateLoan($res){
        (new Slack(new Request(['title'=>"KMGP2P Borrower save update loan Response", 'fields'=>['Message' => json_encode($res)]])))->send();
        if(isset($res->status) && $res->status != 'OK'){
            $this->errorBag['Error'] = $res->message;
            $this->response = $res;
            return $this;
        }
        if(isset($res->error->description)){
            $this->errorBag['Error'] = $res->error->description;
            $this->response = $res;
            return $this;
        }
        if(!isset($res->status)){
            $this->errorBag['Error'] = 'Getting empty response';
            $this->response = $res;
            return $this;
        }

        return $this->response = $res;
    }

    public function saveEmiPayments($res){
        (new Slack(new Request(['title'=>"KMGP2P Borrower save emi payments Response", 'fields'=>['Message' => json_encode($res)]])))->send();
        if(isset($res->status) && $res->status != 'OK'){
            $this->errorBag['Error'] = $res->message;
            $this->response = $res;
            return $this;
        }
        if(isset($res->error->description)){
            $this->errorBag['Error'] = $res->error->description;
            $this->response = $res;
            return $this;
        }
        if(!isset($res->status)){
            $this->errorBag['Error'] = 'Getting empty response';
            $this->response = $res;
            return $this;
        }

        return $this->response = $res;
    }

    public function savePaymentTransactions($res){
        (new Slack(new Request(['title'=>"KMGP2P Borrower save payment transactions Response", 'fields'=>['Message' => json_encode($res)]])))->send();
        if(isset($res->status) && $res->status != 'OK'){
            $this->errorBag['Error'] = $res->message;
            $this->response = $res;
            return $this;
        }
        if(isset($res->error->description)){
            $this->errorBag['Error'] = $res->error->description;
            $this->response = $res;
            return $this;
        }
        if(!isset($res->status)){
            $this->errorBag['Error'] = 'Getting empty response';
            $this->response = $res;
            return $this;
        }

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