<?php


namespace ModulesGarden\ProxmoxAddon\App\Services;


use ModulesGarden\ProxmoxAddon\App\Models\CloudInitScript;
use ModulesGarden\ProxmoxAddon\App\Models\Snippet;
use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
use ModulesGarden\ProxmoxAddon\App\Repositories\ServerConfigurationRepository;
use ModulesGarden\ProxmoxAddon\Core\Traits\Smarty;
use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;

class CloudInitScriptConveter
{
    use Smarty;
    use WhmcsParams;
    use ApiService;

    /**
     * @var CloudInitScript
     */
    protected $cloudInitScript;

    /**
     * @var ServerConfigurationRepository
     */
    protected $serverConfiguration;
    /**
     * @var VmModel|null
     */
    protected $vmModel;

    /**
     * CloudInitScriptConveter constructor.
     * @param CloudInitScript $cloudInitScript
     */
    public function __construct(CloudInitScript $cloudInitScript, ServerConfigurationRepository $serverConfiguration, $vmModel =null)
    {
        $this->cloudInitScript = $cloudInitScript;
        $this->serverConfiguration = $serverConfiguration;
        $this->vmModel = $vmModel;
    }

    /**
     * @return Snippet
     */
    public function convert($params=[]){
        if(!$this->serverConfiguration->snippetDirectory){
            throw new \InvalidArgumentException("Snippet Directory is empty");
        }
        $data=['script','meta','network'];
        if(empty($params)){
            $params = sl('whmcsParams')->getWhmcsParams();
        }
        $params['passwordHash'] = Utility::passwordHash($params['password']);
        if($this->vmModel instanceof VmModel){
            //load ip addresses
            $this->vmModel->ipv4Addresses;
            $this->vmModel->ipv6Addresses;
            $params['vm'] = $this->vmModel->toArray();
            //password decode
            $params['vm']['password'] = $this->vmModel->getPassword();
            $params['vm']['passwordHash'] = Utility::passwordHash($params['vm']['password']);
            $params['ipv4Addresses'] = $params['vm']['ipv4Addresses'];
            $params['ipv6Addresses'] = $params['vm']['ipv6Addresses'];
        }else{
            $params['ipv4Addresses'] = VmIpAddress::ofHostingId($params['serviceid'])->ofIp4()->get()->toArray();
            $params['ipv6Addresses'] = VmIpAddress::ofHostingId($params['serviceid'])->ofIp6()->get()->toArray();
        }
        $snippets=[];
        foreach ($data as $key ){
            if(empty($this->cloudInitScript->{$key})){
                continue;
            }
            $script = html_entity_decode($this->cloudInitScript->{$key}, ENT_QUOTES);
            $name = str_replace('script','userconfig',$key);
            if($this->vmModel instanceof VmModel){
                $filename = sprintf($name."-%s-%s.yaml",$this->getWhmcsParamByKey('serviceid'), $this->vmModel->id);
            }else{//vps
                $filename = sprintf($name."-%s.yaml",$this->getWhmcsParamByKey('serviceid'));
            }
            $content = $this->getSmarty()->fetchString($script, $params);
            $snippets[] = new Snippet($this->serverConfiguration->snippetDirectory,$filename, $content);
        }
        return  $snippets;
    }
}