import logging

from beanstream import billing, errors, transaction
from beanstream.response_codes import response_codes

log = logging.getLogger('beanstream.payment_profiles')


STATUS_DESCRIPTORS = {
        'active' : 'A',
        'closed' : 'C',
        'on hold' : 'O'
}

STATUS_CODES = {
        'A' : 'active',
        'C' : 'closed',
        'O' : 'on hold'
}


class PaymentProfileTransaction(transaction.Transaction):

    def __init__(self, beanstream):
        super(PaymentProfileTransaction, self).__init__(beanstream)
        self.url = self.URLS['payment_profile']
        self.response_class = PaymentProfileResponse

        if not self.beanstream.payment_profile_passcode:
            raise errors.ConfigurationException('payment profile passcode must be specified to create or modify payment profiles')

        self.params['serviceVersion'] = '1.0'
        self.params['merchantId'] = self.beanstream.merchant_id
        self.params['passCode'] = self.beanstream.payment_profile_passcode
        self.params['responseFormat'] = 'QS'

    def set_customer_code(self, customer_code):
        self.params['customerCode'] = customer_code

    def set_language(self, language):
        language = language.upper()
        if language not in ('ENG', 'FRE'):
            raise errors.ValidationException('invalid language option specified: %s (must be one of FRE, ENG)' % language)
        self.params['trnLanguage'] = language

    def set_velocity_id(self, velocity_id):
        self.params['velocityIdentity'] = velocity_id

    def set_status_id(self, status_id):
        self.params['statusIdentity'] = status_id

    def set_status(self, status):
        status = status.lower()

        if status not in STATUS_DESCRIPTORS:
            raise errors.ValidationException('invalid status option specified: %s' % status)

        self.params['status'] = STATUS_DESCRIPTORS[status]


class CreatePaymentProfile(PaymentProfileTransaction):

    def __init__(self, beanstream, card):
        super(CreatePaymentProfile, self).__init__(beanstream)

        self.params['operationType'] = 'N'
        self.params.update(card.params())


class ModifyPaymentProfile(PaymentProfileTransaction):

    def __init__(self, beanstream, customer_code):
        super(ModifyPaymentProfile, self).__init__(beanstream)

        self.params['operationType'] = 'M'
        self.set_customer_code(customer_code)


class GetPaymentProfile(PaymentProfileTransaction):

    def __init__(self, beanstream, customer_code):
        super(GetPaymentProfile, self).__init__(beanstream)

        self.params['operationType'] = 'Q'
        self.set_customer_code(customer_code)


class PaymentProfileResponse(transaction.Response):

    def get_message(self):
        return self.resp.get('responseMessage', [None])[0]

    def customer_code(self):
        return self.resp.get('customerCode', [None])[0]

    def order_number(self):
        return self.resp.get('trnOrderNumber', [None])[0]

    def approved(self):
        return self.resp.get('responseCode', ['0'])[0] == '1'

    def get_message(self):
        return self.resp.get('responseMessage', ['0'])[0]

    def status(self):
        if 'status' in self.resp:
            return STATUS_CODES[self.resp['status'][0]]
        else:
            return None

    def billing_address(self):
        return billing.Address(
            self.resp.get('ordName', [None])[0],
            self.resp.get('ordEmailAddress', [None])[0],
            self.resp.get('ordPhoneNumber', [None])[0],
            self.resp.get('ordAddress1', [None])[0],
            self.resp.get('ordAddress2', [None])[0],
            self.resp.get('ordCity', [None])[0],
            self.resp.get('ordProvince', [None])[0],
            self.resp.get('ordPostalCode', [None])[0],
            self.resp.get('ordCountry', [None])[0],
        )

    def bank_account_type(self):
        return self.resp.get('bankAccountType', [None])[0]

    def card_number(self):
        return self.resp.get('trnCardNumber', [None])[0]

    def expiry_month(self):
        if 'trnCardExpiry' in self.resp:
            return self.resp['trnCardExpiry'][0][:-2]
        else:
            return None

    def expiry_year(self):
        if 'trnCardExpiry' in self.resp:
            return self.resp['trnCardExpiry'][0][-2:]
        else:
            return None


@@ -3933,32 +3933,115 @@
 e', %5BNone%5D)%5B0%5D%0A%0A
+    def card_owner(self):%0A        return self.resp.get('trnCardOwner', %5BNone%5D)%5B0%5D%0A%0A
     def card_num

add card owner to payment profile