<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Coinpayments_Form_Admin_Gateway_Coinpayment extends Payment_Form_Admin_Gateway_Abstract
{
  public function init()
  {
    parent::init();

  $view = Zend_Registry::isRegistered('Zend_View') ? Zend_Registry::get('Zend_View') : null;
  $view->headLink()->appendStylesheet($view->layout()->staticBaseUrl."externals/selectize/css/normalize.css");
  $headScript = new Zend_View_Helper_HeadScript();
  $headScript->prependFile($view->layout()->staticBaseUrl.'externals/jQuery/jquery.min.js');
  $headScript->appendFile($view->layout()->staticBaseUrl.'externals/selectize/js/selectize.js');


  $script = "en4.core.runonce.add(function(){scriptJquery('select[multiple=multiple]').closest('div').css('overflow','visible');scriptJquery('select[multiple=multiple]').selectize({});});";
  $view->headScript()->appendScript($script);

    $this->setTitle('Payment Gateway: Cryptocurrency');
    
    $description = $this->getTranslator()->translate('COINPAYMENTS_FORM_ADMIN_GATEWAY_COINPAYMENT_DESCRIPTION');
    $description = vsprintf($description, array(
      'https://auth.coinpayments.net/Identity/Account/Login?ReturnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3Dcoinpayments-aphrodite-pkce%26response_type%3Dcode%26scope%3Dopenid%2520profile%2520orion%2520offline_access%26redirect_uri%3Dhttps%253A%252F%252Fdashboard.coinpayments.net%252Fcallback%26code_challenge%3Dq3KgMLldG8B_UNIq9MapVfJxuzY6cdOyJevU-3DK6o0%26code_challenge_method%3DS256%26nonce%3Ded9a1594-5bcf-4978-1578-4d11450f1b24',
      'https://a-dashboard.coinpayments.net/en/app/integrations',
      'https://' . $_SERVER['HTTP_HOST'] . Zend_Controller_Front::getInstance()->getRouter()->assemble(array(
          'module' => 'payment',
          'controller' => 'ipn',
          'action' => 'CoinPayments'
        ), 'default', true),
    ));
    $this->setDescription($description);

    // Decorators
    $this->loadDefaultDecorators();
    $this->getDecorator('Description')->setOption('escape', false);


    // Elements
    $this->addElement('Text', 'public_key', array(
      'label' => 'Client ID',
      'filters' => array(
        new Zend_Filter_StringTrim(),
      ),
    ));

    $this->addElement('Text', 'private_key', array(
      'label' => 'Client Secret',
      'filters' => array(
        new Zend_Filter_StringTrim(),
      ),
    ));
    // $this->addElement('Text', 'ipn_secret', array(
    //   'label' => 'IPN Secret',
    //   'filters' => array(
    //     new Zend_Filter_StringTrim(),
    //   ),
    // ));

    // $coins = Engine_Api::_()->coinpayments()->crypto();

    // $this->addElement('multiselect', 'coin', array(
    //   'label' => "Crypto Currencies",
    //   'required' => true,
    //   'allowEmpty' => false,
    //   'description' => 'Please select the crypto currencies in which you want to accept the payments.',
    //   'multiOptions' => $coins,
    //   'value'=>array("LTCT")
    // ));
    // $this->coin->addDecorator('Description', array('placement' => Zend_Form_Decorator_Abstract::PREPEND, 'escape' => false));


    $fileLink = $view->baseUrl() . '/admin/files/';

    $fileOptions = array('' => '');
    $files = Engine_Api::_()->getDbTable('files', 'core')->getFiles(array('fetchAll' => 1, 'extension' => array('gif', 'jpg', 'jpeg', 'png', 'webp', 'svg')));
    foreach( $files as $file ) {
      $fileOptions[$file->storage_path] = $file->name;
    }
    if (engine_count($fileOptions) > 1) {
      $description = $this->getTranslator()->translate('Choose an icon to show with this gateway. This icon will show in both user panel and admin panel of your site. [Note: You can add a new icon from the "<a href="%1$s" target="_blank">File & Media Manager</a>" section. If you leave the field blank then nothing will show.]');
      $description = vsprintf($description, array($fileLink));

      $this->addElement('Select', 'icon', array(
        'label' => "Icon",
        'description' => $description,
        'multiOptions' => $fileOptions,
      ));
      $this->icon->addDecorator('Description', array('placement' => Zend_Form_Decorator_Abstract::PREPEND, 'escape' => false));
    } else {
      $description = $this->getTranslator()->translate('There are currently no icons in the <a href="%1$s" target="_blank"> File & Media Manager </a> section of your site. Please begin by uploading an icon to get started.');
      $description = vsprintf($description, array($fileLink));
      $description = "<div class='tip'><span>" . $description . "</span></div>";
      $this->addElement('Dummy', 'icon', array(
        'label' => "Icon",
        'description' => $description,
      ));
      $this->icon->addDecorator('Description', array('placement' => Zend_Form_Decorator_Abstract::PREPEND, 'escape' => false));
    }

    /*
    $this->addElement('Textarea', 'certificate', array(
      'label' => 'API Certificate',
      'description' => 'You only need to fill in either Signature or ' .
          'Certificate, depending on what type of API account you create.',
    ));
     * 
     */
  }

  public function isValid($values)
  {
    $enabled = (bool) $values['enabled'];
    if( $enabled && ( empty($values['public_key']) || empty($values['private_key'])) ) {
      $this->addError('Please enter the correct API details before enabling this gateway.');
      return false;
    }
    return parent::isValid($values);
  }
}
