from datetime import datetime


class FundsTransferInquiryData(object):
    """Funds Transfer Inquiry data object model.

    :param str pan: **Required**. Primary account number (PAN). 13-19 characters string.
    :param int stan: **Required**. Systems Trace Audit Number. 6 digits integer. Generated by payment application,
        unique within the scope of application until expired (usually within 24 hours). Can be reused if more than
        999 999 transactions were performed during a day.
    :param str rrn: **Optional**. Retrieval Reference Number. 12 digits string. If not provided, will be generated
        as a function from STAN.
    :param int acquiring_bin: **Optional**. BIN under which FundsTransfer app is registered. 6-11 digits integer.
    :param int acquirer_country_code: **Optional**. 3 digits acquirer country code.

    **Example:**
        ..  code-block:: json

            {
                "primaryAccountNumber": "4957030420210512",
                "retrievalReferenceNumber": "330000550000",
                "systemsTraceAuditNumber": "451006"
            }
    """
    ATTR_MAPPINGS = {
        'pan': 'primaryAccountNumber',
        'rrn': 'retrievalReferenceNumber',
        'acquiring_bin': 'acquiringBin',
        'acquirer_country_code': 'acquirerCountryCode',
    }

    def __init__(self, stan, **kwargs):
        self.__dict__.update((self.ATTR_MAPPINGS[k], v) for k, v in kwargs.items() if k in self.ATTR_MAPPINGS and v)

        self.systemsTraceAuditNumber = stan

        try:
            self.retrievalReferenceNumber = kwargs['rrn']
        except KeyError:
            self.retrievalReferenceNumber = self._get_rrn()

    def _get_rrn(self):
        """Generates RRN (retrievalReferenceNumber).

        :return: str rrn: Last digit of current year + number of current day in the year + current hour + STAN
        """
        now = datetime.now()
        year_last_digit = now.strftime('%Y')[3]
        day_of_the_year = now.strftime('%j')
        current_hour = now.strftime('%H')
        return year_last_digit + day_of_the_year + current_hour + str(self.systemsTraceAuditNumber)
