<?php
/**
 * Created by PhpStorm.
 * User: sunil
 * Date: 16/11/18
 * Time: 11:04 AM
 */

namespace App;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class Amortization
{
    private $loan_amount;
    private $term_years;
    private $interest;
    private $terms;
    private $period;
    private $currency = "XXX";
    private $principal;
    private $balance;
    private $term_pay;
    private $paid_term;
    private $emi;
    private $amount_payable;
    private $amount_paid;

    public function __construct($data)
    {
        if($this->validate($data)) {

            $this->loan_amount 	= (float) $data['loan_amount'];
            $this->interest 	= (float) $data['interest'];
            $this->terms 		= (int) $data['terms'];
            $this->paid_term 	= (int) $data['paid_term'];
            $this->emi          =  (int) $data['emi'];
            $this->amount_payable = (int) $data['amount_payable'];
            $this->amount_paid = (int) $data['amount_paid'];

            $this->terms = ($this->terms == 0) ? 1 : $this->terms;
            $this->period = $this->terms;
            if($this->paid_term == "" || $this->paid_term == 0){
                $this->paid_term = $this->terms;
            }
            //$this->interest = ($this->interest);
//            $results = array(
//                'inputs' => $data,
//                'summary' => $this->getSummary(),
//                'shedule' => $this->getShedule(),
//            );

            return json_encode($this->getSummary());
            //$this->getJSON($results);
        }
    }
    private function validate($data) {
        $data_format = array(
            'loan_amount' 	=> 0,
            'interest' 		=> 0,
            'terms' 		=> 0
        );
        $validate_data = array_diff_key($data_format,$data);

        if(empty($validate_data)) {
            return true;
        }else{
            echo "<div style='background-color:#ccc;padding:0.5em;'>";
            echo '<p style="color:red;margin:0.5em 0em;font-weight:bold;background-color:#fff;padding:0.2em;">Missing Values</p>';
            foreach ($validate_data as $key => $value) {
                echo ":: Value <b>$key</b> is missing.<br>";
            }
            echo "</div>";
            return false;
        }
    }
    private function calculate()
    {
        $deno = 1 - 1 / pow((1+ $this->interest),$this->period);

        $this->term_pay = $this->emi ;
        if($this->period == 1){
            $this->term_pay = $this->amount_payable - $this->amount_paid ;
        }
        $interest = ($this->loan_amount * $this->interest)/100;
        $this->principal = $this->term_pay - $interest;
        $this->balance = $this->loan_amount - $this->principal;
        if($this->period == 1){
            $this->balance =  0;
        }
        return array (
            'payment' 	=> $this->term_pay,
            'interest' 	=> $interest,
            'principal' => $this->principal,
            'balance' 	=> $this->balance
        );
    }
    public function getSummary()
    {
        $this->calculate();
        $total_pay = $this->term_pay *  $this->period;
        $total_interest = $total_pay - $this->loan_amount;
        return array (
            'total_pay' => $total_pay,
            'total_interest' => $total_interest,
        );
    }
    public function getShedule ()
    {
        $shedule = array();
        $summary = array();
        while  ($this->balance >= 0 && $this->paid_term > 0) {
            array_push($shedule, $this->calculate());
            $this->loan_amount = $this->balance;
            $this->period--;
            $this->paid_term--;
            $this->amount_paid = $this->amount_paid + $this->emi;
        }
        return $shedule;
    }
    private function getJSON($data)
    {
        header('Content-Type: application/json');
        echo json_encode($data);
    }
}
