<?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\Enabled;

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

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

    const CATEGORY_ID        = 'category_id';
    const NAME               = 'name';
    const DESCRIPTION        = 'description';
    const VIEWS              = 'views';
    const STORE_ID           = 'store_ids';
    const ARTICLES           = 'articles';
    const URL_KEY            = 'url_key';
    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 ENABLED            = 'enabled';
    const META_TITLE         = 'meta_title';
    const META_DESCRIPTION   = 'meta_description';
    const META_KEYWORDS      = 'meta_keywords';
    const META_ROBOTS        = 'meta_robots';
    const POSITION           = 'position';

    /**
     * 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::DESCRIPTION:
                case self::URL_KEY:
                case self::META_TITLE:
                case self::META_DESCRIPTION:
                case self::META_KEYWORDS:
                    $attributeModel->setBackendType('varchar');

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

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

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

        return $this;
    }
}
