<?php
// Copyright 1999-2017. Plesk International GmbH. All rights reserved.

namespace Plesk\MigrationLibV8\MailImport;

use Plesk\MigrationLibV8\Common\Backend\Files\Progress;
use Plesk\MigrationLibV8\Common\SessionDirectory;
use Plesk\MigrationLibV8\MailImport\Backend\Files\Config;
use Plesk\MigrationLibV8\MailImport\Backend\Files\MigrationList;
use Plesk\MigrationLibV8\MailImport\Backend\Files\Status;

class SessionObjects
{
    /** @var \Plesk\MigrationLibV8\MailImport\SessionObjects[] */
    private static $_instances = [];
    /** @var \Plesk\MigrationLibV8\Common\SessionDirectory */
    private $_session;
    /** @var \Plesk\MigrationLibV8\MailImport\SessionFiles|null */
    private $_sessionFiles = null;
    /** @var \Plesk\MigrationLibV8\MailImport\Backend\Files\Config|null */
    private $_config = null;
    /** @var \Plesk\MigrationLibV8\Common\Backend\Files\Progress|null */
    private $_progress = null;
    /** @var \Plesk\MigrationLibV8\MailImport\Backend\Files\MigrationList[]|null */
    private $_mailMigrationLists = [];
    /** @var \Plesk\MigrationLibV8\MailImport\Backend\Files\Status|null */
    private $_status = null;

    /**
     * SessionObjects constructor.
     *
     * @param SessionDirectory $session
     */
    private function __construct(SessionDirectory $session)
    {
        $this->_session = $session;
    }

    /**
     * @param \Plesk\MigrationLibV8\Common\SessionDirectory $session
     * @return \Plesk\MigrationLibV8\MailImport\SessionObjects
     */
    public static function getInstance(SessionDirectory $session)
    {
        if (!array_key_exists($session->getPath(), self::$_instances)) {
            self::$_instances[$session->getPath()] = new self($session);
        }

        return self::$_instances[$session->getPath()];
    }

    /**
     * @return \Plesk\MigrationLibV8\MailImport\Backend\Files\Config
     */
    public function getConfig()
    {
        if (!$this->_config) {
            $configPath = $this->getSessionFiles()->getConfigPath();
            $this->_config = new Config($configPath);
            $this->_config->setDefaultSourceSectionName('mail');
        }

        return $this->_config;
    }

    /**
     * @return \Plesk\MigrationLibV8\Common\Backend\Files\Progress
     */
    public function getProgress()
    {
        if (!$this->_progress) {
            $this->_progress = new Progress(
                $this->getSessionFiles()->getProgressPath()
            );
        }
        return $this->_progress;
    }

    /**
     * @return \Plesk\MigrationLibV8\MailImport\SessionFiles
     */
    public function getSessionFiles()
    {
        if (!$this->_sessionFiles) {
            $this->_sessionFiles = new SessionFiles($this->_session);
        }
        return $this->_sessionFiles;
    }

    /**
     * @param string|null $suffix
     * @return \Plesk\MigrationLibV8\MailImport\Backend\Files\MigrationList
     */
    public function getMailMigrationList($suffix)
    {
        if (!array_key_exists($suffix, $this->_mailMigrationLists)) {
            $this->_mailMigrationLists[$suffix] = new MigrationList(
                $this->getSessionFiles()->getMailMigrationListPath($suffix)
            );
        }
        return $this->_mailMigrationLists[$suffix];
    }

    /**
     * @param string|null $taskId
     * @return Status
     */
    public function getMailMigrationStatus($taskId=null)
    {
        if (!$this->_status) {
            $this->_status = new Status(
                $this->getSessionFiles()->getStatusFilePath($taskId)
            );
        }
        return $this->_status;
    }
}
