<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Agorae
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Webkul\Agorae\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Cms\Model\Page;
use Magento\Cms\Model\PageFactory;
use Magento\Cms\Model\ResourceModel\Page\CollectionFactory as PageCollectionFactory;

/**
 * Agorae InstallData Setup
 */
class InstallData implements InstallDataInterface
{

    /**
     * @var EavSetupFactory $eavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * @var \Magento\Cms\Model\BlockFactory
     */
    protected $pageFactory;
    
    /**
     * @var PageCollectionFactory
     */
    protected $pageCollectionFactory;

    /**
     * @var \Magento\Cms\Model\PageRepository
     */
    protected $pageRepository;

    /**
     * @var \Magento\Framework\App\State
     */
    protected $state;

    /**
     * @var \Webkul\Marketplace\Model\ControllersRepository
     */
    private $controllersRepository;

    /**
     * @var \Magento\Framework\Module\Manager
     */
    protected $moduleManager;

    /**
     * @var \Magento\Framework\App\ObjectManager
     */
    protected $objectManager;

    /**
     * __construct
     * @param EavSetupFactory                                  $eavSetupFactory
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param PageFactory                                      $resultPageFactory
     * @param \Magento\Cms\Model\PageFactory                   $pageFactory
     * @param PageCollectionFactory                            $pageCollectionFactory
     * @param \Magento\Cms\Model\PageRepository                $pageRepository
     * @param \Magento\Framework\App\State                     $state
     * @param \Magento\Framework\Module\Manager                $moduleManager
     */
    public function __construct(
        EavSetupFactory $eavSetupFactory,
        // \Magento\Framework\View\Element\Template\Context $context,
        PageFactory $resultPageFactory,
        \Magento\Cms\Model\PageFactory $pageFactory,
        PageCollectionFactory $pageCollectionFactory,
        \Magento\Cms\Model\PageRepository $pageRepository,
        \Magento\Framework\App\State $state,
        \Magento\Framework\Module\Manager $moduleManager
    ) {
        $this->objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $this->eavSetupFactory = $eavSetupFactory;
        $this->moduleManager = $moduleManager;
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            $this->controllersRepository = $this->objectManager->create(
                \Webkul\Marketplace\Model\ControllersRepository::class
            );
        }
        $this->resultPageFactory = $resultPageFactory;
        $this->pageFactory = $pageFactory;
        $this->pageCollectionFactory = $pageCollectionFactory;
        $this->pageRepository = $pageRepository;
        $state->setAreaCode('frontend');
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        // $eavSetup->removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'wk_image');
        // $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'image_custom_label');
        /**
         * Add attributes to the eav/attribute
         */
        $eavSetup->addAttribute(
            'catalog_category',
            'wk_image',
            [
                'type'                  => 'varchar',
                'group'                 => '',
                'input'                 => 'image',
                'backend'               => \Magento\Catalog\Model\Category\Attribute\Backend\Image::class,
                'frontend'              => '',
                'label'                 => 'Image',
                'note'                  => 'upload a thumbnail image for homepage',
                'source'                => '',
                'global'                => Attribute::SCOPE_GLOBAL,
                'visible'               => true,
                'user_defined'          => false,
                'required'              => false,
                "default"               => "",
                "searchable"            => false,
                "filterable"            => true,
                "comparable"            => false,
                'visible_on_front'      => true,
                'unique'                => false
            ]
        );

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'is_featured_agorae',
            [
                'type' => 'int',
                'backend' => '',
                'frontend' => '',
                'label' => 'Is Featured',
                'input' => 'boolean',
                'class' => '',
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'group' => 'Product Details'
            ]
        );

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'is_seller_pick',
            [
                'type' => 'int',
                'backend' => '',
                'frontend' => '',
                'label' => 'Is Seller\'s Pick',
                'input' => 'boolean',
                'class' => '',
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'group' => 'Product Details'
            ]
        );

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'image_custom_label',
            [
                'type' => 'varchar',
                'backend' => '',
                'frontend' => '',
                'label' => 'Custom Product Images Label',
                'input' => 'select',
                'class' => '',
                'source' => \Webkul\Agorae\Model\Product\Attribute\Source\Type::class,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'group' => 'Product Details'
            ]
        );
        // $this->state->setAreaCode('frontend');
        $collection = $this->pageCollectionFactory->create()
            ->addFieldToFilter('identifier', 'home');
        $homePageId = 0;

        if ($collection->getSize()) {
            foreach ($collection as $value) {
                $homePageId = $value->getPageId();
            }
        }

        if ($homePageId) {
            $content = '{{block class=\Webkul\Agorae\Block\Homepage
                " name="agorae.homepage" template="Magento_Theme::html/homepage.phtml"}}';
            $data = $this->pageFactory->create()->load($homePageId)->setContent($content)->save();
        }
        /*-----Works with marketplace ver 2.0.4 and higher--*/
        $data = [];
        if ($this->moduleManager->isOutputEnabled('Webkul_Marketplace')) {
            if (!count($this->controllersRepository->getByPath('agorae/shopfollower/index'))) {
                $data[] = [
                    'module_name' => 'Webkul_Agorae',
                    'controller_path' => 'agorae/shopfollower/index',
                    'label' => 'Shop Followers',
                    'is_child' => '0',
                    'parent_id' => '0',
                ];
            }
            if (count($data)) {
                $setup->getConnection()
                    ->insertMultiple($setup->getTable('marketplace_controller_list'), $data);
            }
        }
    }
}
