<?php

namespace App\Http\Controllers\Service;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use App\Http\Controllers\Controller;

/*
 * 한국수출입은행
 * https://www.koreaexim.go.kr/
 * 환율 / 대출금리 / 국제금리 정보 제공
 */
class FinancialController extends Controller
{
    const API_EXCHANGE_KEY = 'OiZlh6SjbXGBlfbagekBpi6kAE4UyonP';
    const API_EXCHANGE_HOST = 'https://www.koreaexim.go.kr/site/program/financial/exchangeJSON';
    const API_INTEREST_KEY = '4olJNSqYHe4oVsW50JFXpXyYmDh9O9NN';
    const API_INTEREST_HOST = 'https://www.koreaexim.go.kr/site/program/financial/interestJSON';
    const API_INTERNATIONAL_KEY = 'UI3DgPS3oZucxxCThkJQV6eAGprSbLJy';
    const API_INTERNATIONAL_HOST = 'https://www.koreaexim.go.kr/site/program/financial/internationalJSON';

    public function __construct()
    {
        $this->middleware('front');
    }

    /**
     * 환율 정보
     * @method GET
     * @see /service/financial/exchange
     */
    public function exchange(Request $request)
    {
        $date = $request->get("date", now()->format('Y-m-d'));

        $response = Http::get(self::API_EXCHANGE_HOST, [
            'authkey' => self::API_EXCHANGE_KEY,
            'searchdate' => $date,
            'data' => 'AP01',
        ]);

        $total = 0;
        $list = null;
        if($response->ok()) {
            $list = $response->object();
            $total = count($list);
        }

        return view(layout('service.financial.exchange'), [
            'menuID' => 'exchange',
            'total' => $total,
            'list' => $list,
            'date' => $date
        ]);
    }

    /**
     * 대출금리
     * @method GET
     * @see /service/financial/interest
     */
    public function interest(Request $request)
    {
        $date = $request->get("date", now()->format('Y-m-d'));

        $response = Http::get(self::API_INTEREST_HOST, [
            'authkey' => self::API_INTEREST_KEY,
            'searchdate' => $date,
            'data' => 'AP02',
        ]);

        $total = 0;
        $list = null;
        if($response->ok()) {
            $list = $response->object();
            $total = count($list);
        }

        return view(layout('service.financial.interest'), [
            'menuID' => 'interest',
            'total' => $total,
            'list' => $list,
            'date' => $date
        ]);
    }

    /**
     * 국제금리
     * @method GET
     * @see /service/financial/international
     */
    public function international(Request $request)
    {
        $date = $request->get("date", now()->format('Y-m-d'));

        $response = Http::get(self::API_INTERNATIONAL_HOST, [
            'authkey' => self::API_INTERNATIONAL_KEY,
            'searchdate' => $date,
            'data' => 'AP03',
        ]);

        $sofrList = [];
        $estrList = [];
        $euriborList = [];
        $tonaList = [];
        $tiborList = [];
        $swapRfrList = [];
        $liborList = [];
        $swapList = [];
        $cirrList = [];

        if($response->ok()) {
            $list = $response->object();

            $map = function($list) {
                $ret = [];
                foreach($list as $row) {
                    $ret[$row->cur_fund][] = $row;
                }
                return $ret;
            };

            $sofrList = $list->sofr_list;
            $estrList = $list->estr_list;
            $tonaList = $list->tona_list;

            $euriborList = $map($list->euribor_list);
            $tiborList = $map($list->tibor_list);
            $swapRfrList = $map($list->swapRfr_list);
            $liborList = $map($list->libor_list);
            $swapList = $map($list->swap_list);
            $cirrList = $map($list->cirr_list);
        }

        return view(layout('service.financial.international'), [
            'menuID' => 'international',
            'sofrList' => $sofrList,
            'estrList' => $estrList,
            'euriborList' => $euriborList,
            'tonaList' => $tonaList,
            'tiborList' => $tiborList,
            'swapRfrList' => $swapRfrList,
            'liborList' => $liborList,
            'swapList' => $swapList,
            'cirrList' => $cirrList,
            'date' => $date
        ]);
    }
}
