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

namespace Plesk\MigrationLibV8\SiteImport\Entity\Status\Import;

use Plesk\MigrationLibV8\SiteImport\Entity\CheckStatuses;

class Application
{
    /** @var string */
    private $_type;
    /** @var string */
    private $_sourcePath;
    /** @var string */
    private $_targetPath;
    /** @var \Plesk\MigrationLibV8\SiteImport\Entity\Status\Issue[] */
    private $_issues;
    /** @var string */
    private $_status;
    /** @var \Plesk\MigrationLibV8\SiteImport\Entity\Status\Issue[] */
    private $_checkIssues;
    /** @var string */
    private $_checkStatus;

    /**
     * Application constructor.
     *
     * @param string $type
     * @param string $sourcePath
     * @param string $targetPath
     * @param \Plesk\MigrationLibV8\SiteImport\Entity\Status\Issue[] $issues
     * @param string $status
     * @param \Plesk\MigrationLibV8\SiteImport\Entity\Status\Issue[] $checkIssues
     * @param string $checkStatus
     */
    public function __construct($type, $sourcePath, $targetPath, $issues, $status, $checkIssues, $checkStatus)
    {
        $this->_type = $type;
        $this->_sourcePath = $sourcePath;
        $this->_targetPath = $targetPath;
        $this->_issues = $issues;
        $this->_status = $status;
        $this->_checkIssues = $checkIssues;
        $this->_checkStatus = $checkStatus;
    }

    /**
     * Get application type (e.g. "WordPress", see see \Plesk\MigrationLibV8\SiteImport\Entity\ApplicationTypes constants).
     *
     * @return string see \Plesk\MigrationLibV8\SiteImport\Entity\ApplicationTypes constants
     */
    public function getType()
    {
        return $this->_type;
    }

    /**
     * Get location of the application on the source server.
     * Example: "/var/www/vhosts/source.example.com/httpdocs/wordpress".
     *
     * @return string
     */
    public function getSourcePath()
    {
        return $this->_sourcePath;
    }

    /**
     * Get location of the application on the target server.
     * Example: "/var/www/vhosts/target.example.com/httpdocs/wordpress".
     *
     * @return string
     */
    public function getTargetPath()
    {
        return $this->_targetPath;
    }

    /**
     * @return \Plesk\MigrationLibV8\SiteImport\Entity\Status\Issue[]
     */
    public function getIssues()
    {
        return $this->_issues;
    }

    /**
     * Get import status of the application, as string (e.g. "success")
     *
     * @return string string see constants from \Plesk\MigrationLibV8\SiteImport\Entity\Status\Import\Statuses
     */
    public function getStatus()
    {
        return $this->_status;
    }

    /**
     * @return \Plesk\MigrationLibV8\SiteImport\Entity\Status\Issue[]
     */
    public function getCheckIssues()
    {
        return $this->_checkIssues;
    }

    /**
     * Get check status of the application, as string (e.g. "success")
     *
     * @return string string see constants from \Plesk\MigrationLibV8\SiteImport\Entity\Status\Import\Statuses
     */
    public function getCheckStatus()
    {
        if ($this->_checkStatus == null) {
            $importFinishedStatues = array(
                CheckStatuses::SUCCESS, CheckStatuses::WARNING, CheckStatuses::FAILURE
            );
            if (in_array($this->_status, $importFinishedStatues)) {
                return CheckStatuses::READY_TO_CHECK;
            }
            return CheckStatuses::NOT_IMPORTED;
        }
        return $this->_checkStatus;
    }

    /**
     * Return status information for imported application as array (useful for integration with JavaScript code).
     *
     * Array has the following keys:
     *    'type' - application type (e.g. "WordPress")
     *    'source_path' - location of the application on the source server
     *        (e.g. "/var/www/vhosts/source.example.com/httpdocs/wordpress")
     *    'target_path - location of the application on the target server
     *        (e.g. "/var/www/vhosts/target.example.com/httpdocs/wordpress")
     *    'status' - import status of the application, as string (e.g. "success")
     * @return array
     */
    public function asArray()
    {
        $issues = [];
        foreach ($this->getIssues() as $issue) {
            $issues[] = $issue->asArray();
        }
        $check_issues = [];
        foreach ($this->getCheckIssues() as $check_issue) {
            $check_issues[] = $check_issue->asArray();
        }

        return [
            'type' => $this->getType(),
            'source_path' => $this->getSourcePath(),
            'target_path' => $this->getTargetPath(),
            'issues' => $issues,
            'status' => $this->getStatus(),
            'check_issues' => $check_issues,
            'check_status' => $this->getCheckStatus()
        ];
    }
}
