<?php

namespace App\Method\OneFin\Response;

use App\ConsumerApplicationElement;
use App\Method\OneFin\Helpers\OneFinHelper;
use App\CSV;
use App\OneFinService;

class Response implements OneFinHelper
{
    public $errorBag;
    public $response;
    public $request;
    public function resolveResponse($func, $res, $request = null){
        if(isset($res->error->description)){
            $this->errorBag['Error'] = $res->error->description;
            $this->response = $res;
            return $this;
        }
        if ($request)
            $this->request = $request;
        if(method_exists($this, $func)){
            return $this->$func($res);
        }
        return array();
    }


    public function createLoan($wrapper): Response
    {
        if(empty($wrapper)){
            $this->errorBag['Error'] = 'Invalid server response';
            $this->response = $wrapper;
            return $this;
        }

        $OneFinService = new OneFinService($this->request);
        $OneFinService->updateAPIStatus(__FUNCTION__, $wrapper);

        if(isset($wrapper->is_success) && !$wrapper->is_success){
            $this->errorBag['Error'] = $wrapper->error->message;
            $this->response = $wrapper;
            return $this;
        }

        if(isset($wrapper->data->loan_account_number)) {
            ConsumerApplicationElement::where('application_id', $this->request->get('id'))->update(['onefin_id' => $wrapper->data->loan_account_number, 'onefin_lead_id' => $wrapper->data->lead_id]);
        }
        $this->response = $wrapper;
        return $this;
    }

    public function onBoardChannelEntity($wrapper)
    {
        if(empty($wrapper)){
            $this->errorBag['Error'] = 'Invalid server response';
            $this->response = $wrapper;
            return $this;
        }

        $OneFinService = new OneFinService($this->request);
        $OneFinService->updateAPIStatus(__FUNCTION__, $wrapper);

        if(isset($wrapper->is_success) && !$wrapper->is_success){
            $this->errorBag['Error'] = $wrapper->error->message;
            $this->response = $wrapper;
            return $this;
        }

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

    public function loanDisbursal($wrapper)
    {
        if(empty($wrapper)){
            $this->errorBag['Error'] = 'Invalid server response';
            $this->response = $wrapper;
            return $this;
        }

        $OneFinService = new OneFinService($this->request);
        $OneFinService->updateAPIStatus(__FUNCTION__, $wrapper);

        if(isset($wrapper->is_success) && !$wrapper->is_success){
            $this->errorBag['Error'] = $wrapper->error->message;
            $this->response = $wrapper;
            return $this;
        }
        $this->response = $wrapper;
        return $this;
    }

    public function postLoanRepayment($wrapper)
    {
        if(empty($wrapper)){
            $this->errorBag['Error'] = 'Invalid server response';
            $this->response = $wrapper;
            return $this;
        }

        $OneFinService = new OneFinService($this->request);
        $OneFinService->updateAPIStatus(__FUNCTION__, $wrapper);

        if(isset($wrapper->is_success) && !$wrapper->is_success){
            $this->errorBag['Error'] = $wrapper->error->message;
            $this->response = $wrapper;
            return $this;
        }
        $this->response = $wrapper;
        return $this;
    }

    public function getRepaymentSchedule($wrapper)
    {

        if(empty($wrapper)){
            $this->errorBag['Error'] = 'Invalid server response';
            $this->response = $wrapper;
            return $this;
        }

        $OneFinService = new OneFinService($this->request);
        $OneFinService->updateAPIStatus(__FUNCTION__, $wrapper);

        if(isset($wrapper->is_success) && !$wrapper->is_success){
            $this->errorBag['Error'] = $wrapper->error->message;
            $this->response = $wrapper;
            return $this;
        }
        $this->response = $wrapper;
        return $this;
    }

    public function createMandate($wrapper)
    {

        if(empty($wrapper)){
            $this->errorBag['Error'] = 'Invalid server response';
            $this->response = $wrapper;
            return $this;
        }

        $OneFinService = new OneFinService($this->request);
        $OneFinService->updateAPIStatus(__FUNCTION__, $wrapper);

        if(isset($wrapper->is_success) && !$wrapper->is_success){
            $this->errorBag['Error'] = $wrapper->error->message;
            $this->response = $wrapper;
            return $this;
        }
        $this->response = $wrapper;
        return $this;
    }
}