<?php

namespace App\Services;

use App\Cawangan;
use App\KadarUpah;
use Illuminate\Http\Request as HttpRequest;
use App\Support\OnePercent as OnePercentEngine;
use App\Support\Tawarruq as TawarruqEngine;
use Illuminate\Support\Facades\Log;
class MasterServices
{
    protected $cawangan;
    protected $cawangan_info;
    protected $engine;

    public function __construct(String $kodcaw){
        $this->cawangan = $kodcaw;
        $this->cawangan_info = $this->getInfoCawangan($kodcaw);
        $this->engine = $this->getEngine($kodcaw);
    }

    public function getInfoCawangan(String $kodcaw): Object{
        $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first();
    
        return $cawangan;
    }

    public function getEngine(String $kodcaw){
        if ($this->cawangan_info['version'] == 'v1') {
            return new TawarruqEngine($kodcaw);

        } else if ($this->cawangan_info['version'] == 'v2') {
            return new OnePercentEngine($kodcaw);
        } else {
            return new TawarruqEngine($kodcaw);
        }
    }

    /* Start Gadaian */
    public function createGadai(HttpRequest $request){    
        $response = $this->engine->createGadai($request);

        return $response;
    }

    public function createMarhunGadai($norujukan,HttpRequest $request){
        $response = $this->engine->createMarhunGadai($norujukan,$request);

        return $response;
    }

    public function kiraUpahTawarruq($kodcaw,$norujukan,$jeniskeuntungan){
        if($jeniskeuntungan == 'onepercent'){
            $onepercent = new OnePercentEngine($this->cawangan);
            $response =  $onepercent->getUpahSebenar($norujukan);

        }else{
            $tawarruq = new TawarruqEngine($this->cawangan);
            $response =  $tawarruq->getUpahSebenar($norujukan);
        }

        return $response;
    }

    public function kiraUpahRebet($kodcaw,$norujukan,$jeniskeuntungan){
        if($jeniskeuntungan == 'onepercent'){
            $onepercent = new OnePercentEngine($this->cawangan);
            $response =  $onepercent->getUpahRebet($kodcaw,$norujukan);

        }else{
            $tawarruq = new TawarruqEngine($this->cawangan);
            $response =  $tawarruq->getUpahRebet($kodcaw,$norujukan);
        }

        return $response;
    }

    public function updateGadaiMarhunKaunter($kodcaw, $norujukan, HttpRequest $request){
        if($request->jeniskeuntungan == 'onepercent'){
            $onepercent = new OnePercentEngine($this->cawangan);
            $response =  $onepercent->updateGadaiMarhunKaunter($kodcaw,$norujukan,$request);

        }else{
            $tawarruq = new TawarruqEngine($this->cawangan);
            $response =  $tawarruq->updateGadaiMarhunKaunter($kodcaw,$norujukan,$request);
        }

        return $response;
    }

    public function updateJBGSemasa(HttpRequest $request){
        $onepercent = new OnepercentEngine($this->cawangan);
        $response = $onepercent->updateJBGsemasa($request);
        return $response;
    }

    /* End Gadaian */

    /* Start Penebusan */

    public function tebusTawarruq($kodcaw,$norujukan,$jeniskeuntungan){

        if($jeniskeuntungan == 'onepercent'){
            $onepercent = new OnePercentEngine($this->cawangan);
            $response =  $onepercent->tebusTawarruq($kodcaw,$norujukan,$jeniskeuntungan);
        }else{
            $tawarruq = new TawarruqEngine($this->cawangan);
            $response =  $tawarruq->tebusTawarruq($kodcaw,$norujukan,$jeniskeuntungan);

        }
        return $response;
    }

    public function hargaTebusTawarruq($kodcaw,$norujukan){
        $response = $this->engine->hargaTebusTawarruq($kodcaw,$norujukan);

        return $response;
    }

    public function kiraUpahRebetSebenar($kodcaw,$norujukan){
        $response = $this->engine->kiraUpahRebetSebenar($kodcaw,$norujukan);

        return $response;
    }

    public function upahTawarruq($kodcaw,$norujukan,$jeniskeuntungan){
        if($jeniskeuntungan == 'onepercent'){
            $onepercent = new OnePercentEngine($this->cawangan);
            $response =  $onepercent->upahTawarruq($kodcaw,$norujukan,$jeniskeuntungan);
        }else{
            $tawarruq = new TawarruqEngine($this->cawangan);
            $response =  $tawarruq->upahTawarruq($kodcaw,$norujukan,$jeniskeuntungan);
        }

        return $response;
    }

    public function upahRebetTawarruq($kodcaw,$norujukan,$jeniskeuntungan){
        if($jeniskeuntungan == 'onepercent'){
            $onepercent = new OnePercentEngine($this->cawangan);
            $response =  $onepercent->upahRebetTawarruq($kodcaw,$norujukan,$jeniskeuntungan);
        }else{
            $tawarruq = new TawarruqEngine($this->cawangan);
            $response =  $tawarruq->upahRebetTawarruq($kodcaw,$norujukan,$jeniskeuntungan);

        }
        return $response;
    }

    public function tebusSeparuhTawarruq($kodcaw,$norujukan,HttpRequest $request){
        $response = $this->engine->tebusSeparuhTawarruq($kodcaw,$norujukan,$request);

        return $response;
    }

    public function tebusNotTawarruq($kodcaw,$norujukan){
        $response = $this->engine->tebusNotTawarruq($kodcaw,$norujukan);

        return $response;
    }

    public function hargaTebusNotTawarruq($kodcaw,$norujukan){
        $response = $this->engine->hargaTebusNotTawarruq($kodcaw,$norujukan);

        return $response;
    }

    public function kiraUpahNotTawarruq($kodcaw,$norujukan){
        $response = $this->engine->kiraUpahNotTawarruq($kodcaw,$norujukan);

        return $response;
    }

    public function upahNotTawarruq($kodcaw,$norujukan){
        $response = $this->engine->upahNotTawarruq($kodcaw,$norujukan);

        return $response;
    }

    public function tebusSeparuhNotTawarruq($kodcaw,$norujukan,HttpRequest $request){
        $response = $this->engine->tebusSeparuhNotTawarruq($kodcaw,$norujukan,$request);

        return $response;
    }

    function rebet($kodcaw,$norujukan){
        $response = $this->engine->rebet($kodcaw,$norujukan);

        return $response;
    }

    function rebetselepastawarruq($kodcaw,$norujukan){
        $response = $this->engine->rebetselepastawarruq($kodcaw,$norujukan);

        return $response;
    }

    public function tebusUpdate($kodcaw,$norujukan,HttpRequest $request){

        if($request->jeniskeuntungan == 'onepercent'){
            $onepercent = new OnePercentEngine($this->cawangan);
            $response = $onepercent->tebusUpdate($kodcaw,$norujukan,$request);

        }else{
            $tawarruq = new TawarruqEngine($this->cawangan);
            $response = $tawarruq->tebusUpdate($kodcaw,$norujukan,$request);
        }

        return $response;
    }

    public function tebusUpdateAutoTawarruq($kodcaw,$norujukan,HttpRequest $request){
        if($request->jeniskeuntungan == 'onepercent'){
            $onepercent = new OnePercentEngine($this->cawangan);
            $response = $onepercent->tebusUpdateAutoTawarruq($kodcaw,$norujukan,$request);

        }else{
            $tawarruq = new TawarruqEngine($this->cawangan);
            $response = $tawarruq->tebusUpdateAutoTawarruq($kodcaw,$norujukan,$request);
        }

        return $response;
    }

    public function tebusUpdateTambahPinjaman($kodcaw,$norujukan,HttpRequest $request){
        $onepercent = new OnePercentEngine($this->cawangan);
        $response = $onepercent->tebusUpdateAutoTawarruq($kodcaw,$norujukan,$request);
        return $response;
    }

    public function kiraupahUjrahOnePercent($kodcaw,$norujukan){
        $response = $this->engine->kiraupahUjrahOnePercent($kodcaw,$norujukan);

        return $response;
    }

    public function kiraupahUjrahOnePercentRebate($kodcaw,$norujukan){
        $response = $this->engine->kiraupahUjrahOnePercentRebate($kodcaw,$norujukan);

        return $response;
    }

    public function kiraupahTawarruqByTarikh($kodcaw,$norujukan,$tarikh,HttpRequest $request){
        if($request->jeniskeuntungan == 'onepercent'){
            $onepercent = new OnePercentEngine($this->cawangan);
            $response = $onepercent->kiraanupahTawarruqByTarikh($kodcaw,$norujukan,$tarikh);
            return $response;
        }else{
            $tawarruq = new TawarruqEngine($this->cawangan);
            $response = $tawarruq->kiraanupahTawarruqByTarikh($kodcaw,$norujukan,$tarikh);
            return $response;
        }
    }

    /* End Penebusan */

    public function test(){
        $this->engine->test();
    }
        
}
