import utils  as _utils
import gn_request
from cielo import Cielo



class GetStatusRecurrentCheckout(Cielo):
    
    def __init__(self, *args, **kwargs) :
        
        self.initial_state(**kwargs)
        
        
        self.params = {
            'entrada' : args[0]
        ,   'saida'   : args[1]
        }
        self.method = "GET"
        self.rote = "orders/{merchantId}/{merchantOrderNumber}".format(
                            merchantId          = self.config_ini['cielo_checkout']['{}_MerchantId' .format(kwargs.get('cnpj','')) ]
                        ,   merchantOrderNumber = self.params['entrada']
                    )
        
        
        self.cielo_request = gn_request.Request(
                    base_url = "https://cieloecommerce.cielo.com.br/api/public/v1/"
                ,   headers  = {
                        'MerchantId'    : self.config_ini['cielo_checkout']['{}_MerchantId' .format(kwargs.get('cnpj','')) ]
                    ,   'MerchantKey'   : self.config_ini['cielo_checkout']['{}_MerchantKey'.format(kwargs.get('cnpj','')) ]
                    ,   'Content-Type'  : 'application/json'
                }
            )
        
        super(GetStatusRecurrentCheckout, self, ).__init__(**kwargs)
        
    
    def response_200_(self) :
        
        """ values = [ "{0}={1}".format(key,value)  for key, value in (zip(
            (map(str,self.response.json().keys()  )),
            (map(str,self.response.json().values()))
        )) ] """
        data = self.response.json()
        values = {
                "order_number"                :data.get('order_number','')
            ,   "amount"                      :data.get('amount','')
            ,   "created_date"                :data.get('created_date','')
            ,   "customer_name"               :data.get('customer_name','')
            ,   "customer_phone"              :data.get('customer_phone','')
            ,   "customer_identity"           :data.get('customer_identity','')
            ,   "customer_email"              :data.get('customer_email','')
            ,   "shipping_type"               :data.get('shipping_type','')
            ,   "shipping_price"              :data.get('shipping_price','')
            ,   "payment_method_type"         :data.get('payment_method_type','')
            ,   "payment_method_brand"        :data.get('payment_method_brand','')
            ,   "payment_maskedcreditcard"    :data.get('payment_maskedcreditcard','')
            ,   "payment_installments"        :data.get('payment_installments','')
            ,   "payment_status"              :data.get('payment_status','')
            ,   "tid"                         :data.get('tid','')
            ,   "recurrent_payment_id"        :data.get('recurrent_payment_id','')
            ,   "interval"                    :data.get('interval','')
            ,   "recurrent_status"            :data.get('recurrent_status','')
            ,   "start_date"                  :data.get('start_date','')
            ,   "end_date"                    :data.get('end_date','')
            ,   "test_transaction"            :data.get('test_transaction','')
        }
        values = map(str,values.values())
        self.webservice_cobol(values)
        
    
    def response_400_(self) :
        self.webservice_cobol(['ERROR','400'])
        
    
    def response_404_(self) :
        
        self.webservice_cobol(['ERROR','Nenhum dado encontrado com este id'])
        
        
    