<?php

namespace Benhauer\Salesmanago\Model;

use \Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory as StatusCollectionFactory;

use \SALESmanago\Model\IntegrationInterface;
use \SALESmanago\Entity\Settings;
use \SALESmanago\Model\SettingsInterface;

use \Benhauer\Salesmanago\Model\AbstractModel as SalesmanagoAbstractModel;

class Integration extends SalesmanagoAbstractModel implements IntegrationInterface, SettingsInterface
{
    public function delete($userProperties = null)
    {
        return $this->removePluginConfiguration();
    }

    public function getAccountUserData($userProperties)
    {
        $userData = $this->getDataCoreConfig();

        return [
            Settings::CLIENT_ID => $userData[Settings::CLIENT_ID],
            Settings::ENDPOINT  => $userData[Settings::ENDPOINT],
            Settings::OWNER     => $userData[Settings::OWNER],
            self::WEB_PUSH      => $userData[self::WEB_PUSH],
            self::SYNC_RULE     => isset($userData[self::SYNC_RULE])
                ? filter_var($userData[self::SYNC_RULE], FILTER_VALIDATE_BOOLEAN)
                : false,
            self::API_DOUBLE_OPT_IN => isset($userData[self::API_DOUBLE_OPT_IN])
                ? filter_var($userData[self::API_DOUBLE_OPT_IN], FILTER_VALIDATE_BOOLEAN)
                : false,
            self::DOUBLE_OPT_IN => (isset($userData[self::DOUBLE_OPT_IN]) && !empty($userData[self::DOUBLE_OPT_IN]))
                ? $userData[self::DOUBLE_OPT_IN]
                : false,
            self::STORE_ID => $this->getSMStoreId($this->storeManager->getStore()->getUrl())
        ];
    }

    public function setAccountUserData($userProperties)
    {
        $value = $this
            ->getValue()
            ->addUpdatedAtTime()
            ->extendValue(
                [
                    Settings::OWNER => $userProperties[Settings::OWNER],
                    self::WEB_PUSH  => isset($userProperties[self::WEB_PUSH])
                        ? $userProperties[self::WEB_PUSH]
                        : '',
                    self::SYNC_RULE => $userProperties[self::SYNC_RULE],
                    self::API_DOUBLE_OPT_IN => isset($userProperties[self::API_DOUBLE_OPT_IN])
                        ? filter_var($userProperties[self::API_DOUBLE_OPT_IN], FILTER_VALIDATE_BOOLEAN)
                        : false,
                    self::DOUBLE_OPT_IN => isset($userProperties[self::DOUBLE_OPT_IN])
                        ? $userProperties[self::DOUBLE_OPT_IN]
                        : false
                ]
            )
            ->buildValue();

        return $this->setUserData($value);
    }

    public function getPlatformUserData($userProperties)
    {
        return $userProperties;
    }

    public function setPlatformUserData($userProperties)
    {
        return $userProperties;
    }

    public function getUserSettings($userProperties)
    {
        $data = empty($userProperties)
            ? $this->getDataCoreConfig()
            : $this->getDataCoreConfig();

        return $data;
    }

    /**
     * Get Magento order statuses list and compare it with selected by client
     * @return array - Statuses lists
     */
    public function getMatchedOrderStatuses(StatusCollectionFactory $statusCollectionFactory)
    {
        $statuses = $statusCollectionFactory
            ->create()
            ->toOptionArray();

        foreach ($statuses as $key => $status) {
            $status['status'] = $status['value'];
            unset($status['value']);
            $status['selected'] = $this->checkIfOrderStatusChoosed($status['status']);
            $statuses[$key] = $status;
        }

        return array('statuses' => $statuses);
    }

    /**
     * Set Order statuses to settings
     * @param array $params order statuses
     * @return boolean
     */
    public function setOrderStatuses($params = null)
    {
        if (empty($params) && $params != '') {
            return false;
        }

        $data = $this->getDataCoreConfig();
        $data[self::O_TYPES] = $params;

        $this->update('', $data);

        return true;
    }

    /**
     * Get order statuses chosen by admin for sm plugin configuration & check if $param exist in there
     * @param string one of order status
     * @return boolean
     */
    public function checkIfOrderStatusChoosed($param)
    {
        $data = $this->getDataCoreConfig();

        if (!isset($data[self::O_TYPES])
            || empty($data[self::O_TYPES])
        ) {
            return false;
        }

        return in_array($param, array_values($data[self::O_TYPES]));
    }
}
