<?php
/**
 * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

namespace Oney\ThreeByFour\Block\Adminhtml\Info;

use Magento\Backend\Block\Template;
use Oney\ThreeByFour\Api\Marketing\BusinessTransactionsInterface;

class ThreeByFour extends \Magento\Config\Block\System\Config\Form\Field
{
    const ERROR_MESSAGE = "oney_error_message_admin";
    const WARNING_MESSAGE = "oney_warning_message_admin";
    const SUCCESS_MESSAGE = "oney_success_message_admin";
    const ERROR_CLASS = "oney error";
    const WARNING_CLASS = "oney warning";
    const SUCCESS_CLASS = "oney success";

    /**
     * @var string
     */
    protected $error = "";
    /**
     * @var BusinessTransactionsInterface
     */
    protected $businessTransactions;

    public function __construct(
        Template\Context $context,
        BusinessTransactionsInterface $businessTransactions,
        array $data = [])
    {
        parent::__construct($context, $data);
        $this->businessTransactions = $businessTransactions;
    }

    public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
    {
        $html = $this->_layout
            ->createBlock(self::class)
            ->setTemplate('Oney_ThreeByFour::system/config/admin_info.phtml')
            ->setCacheable(false)
            ->toHtml();

        return $html;
    }

    public function getMessageClass()
    {
        $error = $this->businessTransactions->getResponse();

        if ($error === 200) {
            return self::SUCCESS_CLASS;
        }
        if(in_array($error, [400, 401], false)) {
            return self::WARNING_CLASS;
        }
        return self::ERROR_CLASS;
    }

    public function getMessageContent()
    {
        $error = $this->businessTransactions->getResponse();

        if($error === 200) {
            return self::SUCCESS_MESSAGE;
        }
        if(in_array($error, [400, 401], false)) {
            return self::WARNING_MESSAGE;
        }
        return self::ERROR_MESSAGE;
    }
}


