<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Helper\SmartyHelper;

use \Illuminate\Database\Capsule\Manager as DB;
use \ModulesGarden\HostingQuotaNotifications\Core\Helper;

class Domain
{
    private $data;
    private $output;

    public function getUserID()
    {
        return $this->data['userid'];
    }

    public function __construct($id, $langHelper = false)
    {
        //$this->langHelper = $langHelper;

        $domain = DB::table('tbldomains')
                ->select("tbldomains.*")
                ->where('id', $id)
                ->first();

        $this->data = (array) $domain;

        $expirydays_todaysdate  = strtotime(date("Ymd"));
        $expirydays_expirydate  = strtotime($domain->expirydate);
        $expirydays             = round(($expirydays_expirydate - $expirydays_todaysdate) / 86400);
        $expirydays_nextduedate = strtotime($domain->nextduedate);
        $nextduedays            = round(($expirydays_nextduedate - $expirydays_todaysdate) / 86400);

        if ($expirydays < 0)
            $expirydays = $nextduedays;

        if ($expirydays < 0)
        {
            $expirydays  = 0;
            $nextduedays = 0;
        }

        $this->data['afterexpirydays'] = round(($expirydays_todaysdate - $expirydays_expirydate) / 86400);
        ;
        $this->data['expirydays']      = $expirydays;
        $this->data['nextduedays']     = $nextduedays;

        $domainparts       = explode(".", $domain->domain, 2);
        $this->data['sld'] = $domainparts[0];
        $this->data['tld'] = $domainparts[1];
    }

    public function getFormatedData()
    {
        global $currency;
        $currency = getCurrency($this->data["userid"]);

        $this->output = $this->data;
//        $this->output['status'] = MG_SMS_WHMCSLangHelper::get(
//            "clientarea" . strtolower(str_replace(" ", "", $this->output['status'])), 
//            $this->getProperLangForTemplate());

        $array = array("registrationdate", "expirydate", "nextduedate");
        foreach ($array as $v)
        {
            if (isset($this->output[$v]))
                $this->output[$v] = fromMySQLDate($this->output[$v], 0, 1);
        }

        $this->output['firstpaymentamount'] = formatCurrency($this->output['firstpaymentamount']);
        $this->output['recurringamount']    = formatCurrency($this->output['recurringamount']);
//        $this->output['registrationperiod'] = $this->output['registrationperiod']." "
//            .MG_SMS_WHMCSLangHelper::get('orderyears', $this->getProperLangForTemplate());

        $this->output['dnsmanagement']   = ($this->output['dnsmanagement'] ? "1" : "0");
        $this->output['emailforwarding'] = ($this->output['emailforwarding'] ? "1" : "0");
        $this->output['idprotection']    = ($this->output['idprotection'] ? "1" : "0");
        $this->output['donotrenew']      = ($this->output['donotrenew'] ? "1" : "0");

        return $this->output;
    }

    public function getMergeFields()
    {
        $map      = self::getMergeFieldsMap();
        $formated = $this->getFormatedData();
        $fields   = array();

        foreach ($map as $k => $v)
            $fields[$k] = $formated[$v];

        return $fields;
    }

    public static function getMergeFieldsNames()
    {
        return array_keys(self::getMergeFieldsMap());
    }

    private static function getMergeFieldsMap()
    {
        return [
            'domain_id'                   => 'id',
            'domain_order_id'             => 'orderid',
            'domain_reg_date'             => 'registrationdate',
            'domain_status'               => 'status',
            'domain_name'                 => 'domain',
            'domain_sld'                  => 'sld',
            'domain_tld'                  => 'tld',
            'domain_first_payment_amount' => 'firstpaymentamount',
            'domain_recurring_amount'     => 'recurringamount',
            'domain_registrar'            => 'registrar',
            'domain_reg_period'           => 'registrationperiod',
            'domain_expiry_date'          => 'expirydate',
            'domain_next_due_date'        => 'nextduedate',
            'domain_days_until_expiry'    => 'expirydays',
            'domain_days_until_nextdue'   => 'nextduedays',
            'domain_dns_management'       => 'dnsmanagement',
            'domain_email_forwarding'     => 'emailforwarding',
            'domain_id_protection'        => 'idprotection',
            'domain_do_not_renew'         => 'donotrenew',
            'domain_type'                 => 'type',
            'domain_days_after_expiry'    => 'afterexpirydays',
        ];
    }
}
