<?php

namespace App\Method\Vivriti\Response;

use App\Method\Vivriti\Helpers\VivritiHelper;
use App\VivritiLoans;

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


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

    public function createLoan($wrapper)
    {
        if (isset($wrapper->error->message) || isset($wrapper->error->description)) {
            $this->errorBag['Error'] = $wrapper->error->message ?? $wrapper->error->description;
            return $this;
        }

        if (isset($wrapper->client_loan_id)) {
            $loan = VivritiLoans::where('application_id', request()->get('id'))->first(['id']);
            if (!isset($loan->id)) {
                $loan = new VivritiLoans();
                $loan->fill([
                    'application_id' => request()->get('id'),
                    'loan_id' => $wrapper->client_loan_id,
                    'status' => $wrapper->status
                ]);
                $loan->save();

                if ($wrapper->status == 'rejected'){
                    saveComment("Vivriti Loan Status : " . $wrapper->status, request()->get('id'), 107);
                }
            }
        }

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

    public function updateLoan($wrapper)
    {
        if (isset($wrapper->error->message) || isset($wrapper->error->description)) {
            $this->errorBag['Error'] = $wrapper->error->message ?? $wrapper->error->description;
            return $this;
        }

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

    public function agreementSigned($wrapper)
    {
        if (isset($wrapper->error->message) || isset($wrapper->error->description)) {
            $this->errorBag['Error'] = $wrapper->error->message ?? $wrapper->error->description;
            return $this;
        }

        if ($wrapper->status){
            $loan =  VivritiLoans::where('loan_id', $wrapper->client_loan_id)->first(['application_id']);
            if (isset($loan->application_id)) {
                saveComment("Vivriti Status : Agreement Signed Status: " . $wrapper->status, $loan->application_id, 107);
            }
        }

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

    public function createRepayment($wrapper)
    {
        if (isset($wrapper->error->message) || isset($wrapper->error->description)) {
            $this->errorBag['Error'] = $wrapper->error->message ?? $wrapper->error->description;
            return $this;
        }

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

    public function getLoan($wrapper)
    {
        if (isset($wrapper->error->message) || isset($wrapper->error->description)) {
            $this->errorBag['Error'] = $wrapper->error->message ?? $wrapper->error->description;
            return $this;
        }

        return $this;
    }
}