<?php namespace TPS\Birzha\Components;

use Cms\Classes\ComponentBase;
use Illuminate\Support\Facades\Config;
use October\Rain\Exception\AjaxException;
use October\Rain\Support\Facades\Event;
use TPS\Birzha\Models\Payment;

class Balance extends ComponentBase
{
    public function componentDetails()
    {
        return [
            'name'        => 'Balance',
            'description' => 'Balance'
        ];
    }

    /**
     * On online payment
     */
    public function onSend() {
        $data = post();

        $rules = [
            'amount' => 'required|numeric',
            'card_type' => 'required_if:payment_type,online|in:halkbank,rysgal,senagat',
        ];

        $this->validateForm($data, $rules);

        switch ($data['payment_type']) {
            case 'bank':
                $this->page['amount'] = $data['amount'];
                return [
                    '#form-steps' => $this->renderPartial('@bank_transfer_pay')
                ];
            case 'online':
                $url = $this->payOnline($data);
                if(!$url) {
                    $this->page['err_message'] = trans('validation.balance.bank_service_unavailable');
                    return [
                        '#form-steps' => $this->renderPartial('@message')
                    ];
                }

                $this->page['url'] = $url;
                return [
                    '#form-steps' => $this->renderPartial('@redirect')
                ];
        }
    }

    protected function payOnline($formData) {
        $payment = $this->createNewPayment(false, $formData);

        $url = $this->controller->pageUrl('bank_result.htm', ['payment_id' => $payment->id]);

        // Halkbank or Rysgal or Senagat
        $onlinePaymentClass = Config::get('bank.' . $formData['card_type'] . '.class');
        $onlinePayment = new $onlinePaymentClass;

        $response = $onlinePayment->registerOrder($payment, $url);
        
        $result = json_decode($response->body,true);

        $successfullyRegistered = $onlinePayment->checkRegisterOrderErrorCode($result);
        if($successfullyRegistered) {

            $payment->order_id = $result['orderId'];

            $payment->save();

            return $result['formUrl'];

        } else {
            return false;
        }

    }

    /**
     * On bank transfer payment
     */
    public function onPayByBankTransfer() {
        $data = input();

        $rules = [
            'bank_file' => 'required|mimes:pdf,jpg,png',
        ];

        $this->validateForm($data, $rules);

        $newPayment = new Payment;
        $newPayment->user_id = \Auth::user()->id;
        $newPayment->amount = 0;
        $newPayment->payment_type = "bank";


        // attach file to payment
        $newPayment->bank_file = \Input::file('bank_file');
        if($newPayment->save()){
            Event::fire('tps.payment.received',$newPayment);
            return [
                '#form-steps' => $this->renderPartial('@payment_finish')
            ];
        }else{
            throw new AjaxException('Toleg kabul edilmedi.');
        }

    }

    protected function validateForm($data, $rules) {
        $validator = \Validator::make($data, $rules);

        if($validator->fails()) {
            throw new \ValidationException($validator);
        }
    }

    protected function createNewPayment($bank_file, $formData) {
        $newPayment = new Payment;
        $newPayment->user_id = \Auth::user()->id;
        $newPayment->amount = $formData['amount'];
        $newPayment->payment_type = $formData['payment_type'];
        $newPayment->card_type = isset($formData['card_type']) ? $formData['card_type'] : null;
        $newPayment->save();

        // attach file to payment
        if($bank_file) {
            $newPayment->bank_file = $bank_file;
            $newPayment->save();
        }

        return $newPayment;
    }
}
