# coding=utf-8
import base64

from sysdefs import eft
from modelcommon.model import SitefResponse
from typing import (
    Any,
    Union
)

from model import (
    CancelPaymentOrderData,
    ConfirmFinishPaymentData,
    EftData,
    PaymentResponseData
)
from paymentprocessor import PaymentProcessor
from util import get_transaction_identifier


class FakePaymentProcessor(PaymentProcessor):
    def connection_status(self, test_connection_data):
        return True

    def add_payment(self, start_payment_data):
        pos_id = start_payment_data.pos_id
        session_id = start_payment_data.session_id
        transaction_identifier = get_transaction_identifier(
            order_id=start_payment_data.order_id,
            tender_id=start_payment_data.electronic_type
        )
        self.register_eft_activity(EftData(pos_id, session_id, eft.EFT_CREDITSALE, 1,
                                           start_payment_data.amount, orderid=start_payment_data.order_id))
        self.dialog_manager.close_dialogs(pos_id)
        return PaymentResponseData(
            media="",
            result="0",
            card_number="4111XXXXXXXXXXXX",
            auth_code="1302",
            approved_amount=start_payment_data.amount,
            id_auth="08496898000142",
            cnpj_auth="68536379000192",
            bandeira="4",
            receipt_merchant=base64.b64encode("Recibo de teste.\n MERCHANT\nSitef FAKE"),
            receipt_customer=base64.b64encode("Recibo de teste.\n CUSTOMER\nSitef FAKE"),
            expiration_date="05/2029",
            nsu="1234",
            transaction_processor="00051",
            last_digits="1111",
            sat_acquirer="012",
            nfce_brand="01",
            original_nfce_brand="01",
            nsu_host="4321",
            electronic_type=start_payment_data.electronic_type,
            transaction_identifier=transaction_identifier
        )

    def cancel_payment_order(
        self,
        cancel_payment_order_data   # type: CancelPaymentOrderData
    ):
        # type: (...) -> Any[Union[SitefResponse, str]]

        response = SitefResponse()
        response.nsu = "123"
        response.nsu_host = "321"
        response.nsu_origin = "555"
        response.transaction_value = "10"
        response.customer_receipt = base64.b64encode("Recibo de teste CANCELAMENTO.\n CUSTOMER\nSitef FAKE")
        response.merchant_receipt = base64.b64encode("Recibo de teste CANCELAMENTO.\n MERCHANT\nSitef FAKE")
        response.transaction_identifier = get_transaction_identifier(
            order_id=cancel_payment_order_data.order_id,
            tender_id=cancel_payment_order_data.tender_id
        )
        return response

    def cancel_payment(self):
        return ""

    def finish_payment(self, finish_payment_data):
        return True

    def confirm_finish_payment(
        self,
        confirm_finish_payment_data     # type: ConfirmFinishPaymentData
    ):
        # type: (...) -> None

        pass

    def get_name(self):
        return "FakeElectronicPayment"
