<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Faqs
 * @copyright   Copyright (c) Mageplaza (http://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */
namespace Mageplaza\Faqs\Model\Export;

use Exception;
use Magento\Customer\Model\Customer\Attribute\Source\Store;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Eav\Model\Entity\AttributeFactory;
use Magento\Framework\Data\Collection;
use Magento\Framework\Data\Collection\EntityFactoryInterface;
use Mageplaza\Faqs\Model\Entity\Attribute\Source\Status;
use Mageplaza\Faqs\Model\Entity\Attribute\Source\Visibility;

/**
 * Class AttributeCollection
 * @package Mageplaza\Faqs\Model\Export
 */
class AttributeCollection extends Collection
{
    /**
     * @var AttributeFactory
     */
    protected $attributeFactory;

    /**
     * @var array
     */
    protected $mapLabels = [
        'Question',
        'Author Name',
        'Author Email',
        'Status',
        'Visibility',
        'Article Content',
        'Store IDs',
        'URL Key',
        'Meta Title',
        'Meta Description',
        'Meta Keywords'
    ];

    const ARTICLE_ID         = 'article_id';
    const NAME               = 'name';
    const AUTHOR_NAME        = 'author_name';
    const AUTHOR_EMAIL       = 'author_email';
    const STATUS             = 'status';
    const VISIBILITY         = 'visibility';
    const ARTICLE_CONTENT    = 'article_content';
    const CATEGORIES         = 'categories';
    const TAGS               = 'tags';
    const PRODUCTS           = 'products';
    const RELATED_ARTICLES   = 'related_articles';
    const STORE_ID           = 'store_ids';
    const POSITIVES          = 'positives';
    const NEGATIVES          = 'negatives';
    const VIEWS              = 'views';
    const POSITION           = 'position';
    const ICON_TYPE          = 'icon_type';
    const ICON               = 'icon';
    const ICON_CUSTOM_PATH   = 'icon_custom_path';
    const ICON_CUSTOM_WIDTH  = 'icon_custom_width';
    const ICON_CUSTOM_HEIGHT = 'icon_custom_height';
    const URL_KEY            = 'url_key';
    const IN_RSS             = 'in_rss';
    const EMAIL_NOTIFY       = 'email_notify';
    const META_TITLE         = 'meta_title';
    const META_DESCRIPTION   = 'meta_description';
    const META_KEYWORDS      = 'meta_keywords';
    const META_ROBOTS        = 'meta_robots';

    /**
     * AttributeCollection constructor.
     *
     * @param EntityFactoryInterface $entityFactory
     * @param AttributeFactory $attributeFactory
     */
    public function __construct(
        EntityFactoryInterface $entityFactory,
        AttributeFactory $attributeFactory
    ) {
        $this->attributeFactory = $attributeFactory;

        parent::__construct($entityFactory);
    }

    /**
     * @param array $attributes
     *
     * @return $this
     * @throws Exception
     */
    public function buildAttributes($attributes = [])
    {
        foreach ($attributes as $key => $attribute) {
            /** @var Attribute $attributeModel */
            $attributeModel = $this->attributeFactory->create();
            $attributeModel->setId($attribute);

            switch ($attribute) {
                case self::STORE_ID:
                    $attributeModel->setSourceModel(Store::class);
                    $attributeModel->setFrontendInput('select');

                    break;
                case self::NAME:
                case self::ARTICLE_CONTENT:
                case self::URL_KEY:
                case self::AUTHOR_NAME:
                case self::AUTHOR_EMAIL:
                case self::META_TITLE:
                case self::META_DESCRIPTION:
                case self::META_KEYWORDS:
                    $attributeModel->setBackendType('varchar');

                    break;
                case self::VISIBILITY:
                    $attributeModel->setSourceModel(Visibility::class);
                    $attributeModel->setFrontendInput('boolean');

                    break;
                case self::STATUS:
                    $attributeModel->setSourceModel(Status::class);
                    $attributeModel->setFrontendInput('boolean');

                    break;
                default:
                    $attributeModel->setBackendType('int');
            }

            $attributeModel->setDefaultFrontendLabel($this->mapLabels[$key]);
            $attributeModel->setAttributeCode($attribute);
            $this->addItem($attributeModel);
        }

        return $this;
    }
}
