<?php
loadcontroller('base/module');

class ControllerPaymentPagarme extends ControllerBaseModule {

    protected $referencePathFile = 'payment/pagarme';

    protected $fieldsToRemoveMask = [
      'pagarme_billet.valor_minimo' => 'removeMaskToDouble',
      'pagarme_billet.desconto' => 'removeMaskToDouble',
      'pagarme_parcela_minima' => 'removeMaskToDouble',
      'pagarme_parcela_juros' => 'removeMaskToDouble',
      'pagarme_parcela_desconto' => 'removeMaskToDouble',
    ];
    protected $arrayJson = [
      'pagarme_credit_card',
      'pagarme_pix',
      'pagarme_billet',
    ];

    protected function loadData()
    {
        parent::loadData();

        $decript = [
            "pagarme_client_public",
            "pagarme_client_id",
            "pagarme_client_secret",
        ];

        foreach ($decript as $value){
          if(isset($this->dataVue['configurations'][$value])){
            $this->dataVue['configurations'][$value] = Crypto::descriptografarAES256($this->dataVue['configurations'][$value], Crypto::CAES256);
            $this->dataVue['configurations'][$value] = $this->dataVue['configurations'][$value] ? $this->dataVue['configurations'][$value] : '';
          }
        }

        $this->load->type('tipocomum');
        $this->load->type('tipofrete');

        $this->dataVue['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

        if ($this->config->get('processo_venda_status')){
            $this->dataVue['order_statuses'] = $this->dmodel_proccess_order_to_situation->getProccessStatusAll();
        } else {
            $this->dataVue['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
        }

        $this->dataVue['Type_CadastroPessoa']    = Type_CadastroPessoa::getEnumList();
        $this->dataVue['Type_Status']            = Type_Status::getEnumList();
        $this->dataVue['Type_DsimDnao']          = Type_DsimDnao::getEnumList();
        $this->dataVue['Type_Environment']       = Type_Environment::getEnumList();
        $this->dataVue['Type_AcrescimoDesconto'] = Type_AcrescimoDesconto::getEnumList();
        $this->dataVue['Type_MarketplaceAdjustmentPrice'] = Type_MarketplaceAdjustmentPrice::getEnumList();
        $this->dataVue['Type_DisplayModuleOption'] = Type_DisplayModuleOption::getEnumList();

        $this->dataVue['TypesForJs'] = array(
            Type_ConfigParcelamento::getTypeForJs(),
            Type_CalculoParcelamento::getTypeForJs(),
        );

        $this->dataVue['url_callback'] = $this->urlloja->link('', '', 'NONSSL', false) . 'pagarmecallback';
        $this->dataVue['url_validate_credentials'] = $this->url->link('payment/pagarme/validateCredentials', 'token=' . $this->session->data['token'], 'SSL');
        $this->dataVue['url_parcelamento_config'] = $this->url->link('payment/pagarme/getParcelamentoAvancado', 'token=' . $this->session->data['token'], 'SSL');
    }

    protected function loadModel(){
        parent::loadModel();
        $this->load->model('localisation/geo_zone');
        $this->load->dmodel('proccess/order_to_situation');
        $this->load->model('localisation/order_status');
    }

    public function validateCredentials(){

        $result = $this->pagarme->getAccessToken();
    }

    public function save()
    {
      // atualizando campos de forma dinamica que terão que ter a mascara removida
      for ($x=0; $x<=$this->request->post['pagarme_parcela_qtdmax']; $x++){
        $this->fieldsToRemoveMask['pagarme_parcelamento_configs.' . $x . '.val_minimo'] = 'removeMaskToDouble';
        $this->fieldsToRemoveMask['pagarme_parcelamento_configs.' . $x . '.percentual_calculo'] = 'removeMaskToDouble';
      }
      for ($x=0; $x<=4; $x++){
        $this->fieldsToRemoveMask['pagarme_credit_card.' . $x . '.valor_minimo'] = 'removeMaskToDouble';
      }
        $this->request->post['pagarme_client_public']   = Crypto::criptografarAES256($this->request->post['pagarme_client_public'],Crypto::CAES256);
        $this->request->post['pagarme_client_secret']   = Crypto::criptografarAES256($this->request->post['pagarme_client_secret'],Crypto::CAES256);
        $this->request->post['pagarme_client_id']       = Crypto::criptografarAES256($this->request->post['pagarme_client_id'],Crypto::CAES256);

        parent::save();
    }

    public function install() {
      $this->load->model('setting/extension');
      $this->load->model('setting/setting');

      $this->load->type('tipopagarme');

      $this->model_setting_setting->insertSettingValue($this->fileName, 'pagarme_credit_card' ,json_decode(Type_PagarmeInstallBank::Tp_install), 0,$this->arrayJson);
      $this->model_setting_setting->insertSettingValue($this->fileName, 'pagarme_client_id'     ,'', 0);
      $this->model_setting_setting->insertSettingValue($this->fileName, 'pagarme_client_public' ,'', 0);
      $this->model_setting_setting->insertSettingValue($this->fileName, 'pagarme_client_secret' ,'', 0);
      $this->model_setting_setting->insertSettingValue($this->fileName, 'pagarme_cnpj' ,'', 0);

      $this->model_setting_extension->install('total', 'pagarme', Type_ExtensionSubType::Tp_TotalizadorAcrescimoDesconto);
    }

    public function uninstall(){
        $this->load->model('setting/extension');

        $this->model_setting_extension->uninstall('total', 'pagarme');
    }

}