<?php
namespace Dreamfox\DeliveryDate\Admin;

use Dreamfox\DeliveryDate\Abstracts\AbstractAssets;
use Dreamfox\DeliveryDate\Facades\SystemFacade;

class AssetsManager extends AbstractAssets
{
  const BUILD_TYPE = 'free';
  protected $_buildUrl;
  protected $_settings;

  public function __construct()
  {
    $jsonFilePath = sprintf('%s/assets/%s/build/asset-manifest.json', DREAMFOX_WDD_ABSPATH, $this->_getBuildType());
    $this->_settings = json_decode(file_get_contents( $jsonFilePath ), true);
    $this->_buildUrl = sprintf('%s/build', $this->_getBuildType());
  }

  protected function _getBuildType()
  {
    return self::BUILD_TYPE;
  }

  protected function _getEventName()
  {
    return 'admin_enqueue_scripts';
  }

  protected function _buildStyles()
	{
    $cssFilePath = $this->_buildUrl . $this->_settings['files']['main.css'];
		return [
			'base' => array(
				'src'     => $this->getAssetUrl( 'admin.css' ),
			),
			'style' => array(
				'src'     => $this->getAssetUrl( $cssFilePath ),
			),
		];
	}

  protected function _buildScripts()
	{
    $jsFilePath = $this->_buildUrl . $this->_settings['files']['main.js'];

		return [
			'script' => array(
				'src'     => $this->getAssetUrl( $jsFilePath ),
        'localizer' => $this->_buildLocalizerParams(),
			),
		];
	}

  protected function _buildLocalizerParams()
  {
    $facade = SystemFacade::getInstance();
    $settings = $facade->loadSettings();
    return [
      'params' => [
        'categories'          => $settings->getCategories(),
        'deliveryDays'        => $settings->getDeliveryDays(),
        'assetImageUrl'       => $this->getAssetUrl('/images'),
        // Webpack publicPath: bundled images resolve against this at runtime.
        'buildUrl'            => $this->getAssetUrl( $this->_getBuildType() . '/build/' ),
        'nonce'               => wp_create_nonce( 'dreamfox_wdd_ajax' ),
        'isPremium'           => dreamfox_delivery_date()->is__premium_only(),
        'upgradeUrl'          => $this->_getUpgradeUrl(),
        'premiumFeaturesUrl'  => $this->_getPremiumFeaturesUrl(),
        'supportUrl'          => $this->_getSupportUrl(),
        'contactUrl'          => $this->_getContactUrl(),
        'kbUrl'               => 'https://help.dreamfoxmedia.com/delivery-date-for-woocommerce',
        'faqUrl'              => 'https://wordpress.org/plugins/woocommerce-delivery-date/#faq',
        'wpSupportUrl'        => 'https://wordpress.org/support/plugin/woocommerce-delivery-date/',
      ],
    ];
  }

  protected function _getUpgradeUrl()
  {
    return $this->_getPricingUrl();
  }

  protected function _getPremiumFeaturesUrl( $medium = 'information_tab' )
  {
    return $this->_getPricingUrl();
  }

  /**
   * Freemius pricing page ({menu-slug}-pricing).
   */
  protected function _getPricingUrl()
  {
    return admin_url( 'admin.php?page=woocommerce-delivery-date-pricing' );
  }

  /**
   * External support form URL (free build, or fallback).
   */
  protected function _getSupportUrl()
  {
    return add_query_arg(
      array(
        'utm_source'   => 'plugin',
        'utm_medium'   => 'support_tab',
        'utm_campaign' => 'dfm-wdd-f',
      ),
      'https://dreamfoxmedia.com/support'
    );
  }

  /**
   * External support form URL (free and premium).
   */
  protected function _getContactUrl()
  {
    return $this->_getSupportUrl();
  }
}