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

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * @var \Magento\Framework\Filesystem\Driver\File
     */
    protected $fileDriver;
    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger;

    /**
     * @param \Magento\Framework\Filesystem\Driver\File $fileDriver
     * @param \Psr\Log\LoggerInterface $logger
     */
    public function __construct(
        \Magento\Framework\Filesystem\Driver\File $fileDriver,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->fileDriver = $fileDriver;
        $this->logger = $logger;
    }
    
    /**
     * Install method
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $this->moveDirToMediaDir();
        $this->moveCategoryImagesToMediaDir();
        $installer = $setup;
        $installer->startSetup();
        /*
         * Create table 'webkul_agorae_images'
         */
        $table = $installer->getConnection()
            ->newTable($installer->getTable('webkul_agorae_images'))
            ->addColumn(
                'entity_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                11,
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                'Image Id'
            )->addColumn(
                'title',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => false, 'default' => ''],
                'Image title'
            )->addColumn(
                'filename',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => false, 'default' => ''],
                'Image Name'
            )->addColumn(
                'link',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => false, 'default' => ''],
                'Image Link'
            )->addColumn(
                'status',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                11,
                ['nullable' => false, 'default' => '0'],
                'Status'
            )->addColumn(
                'sort_order',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                11,
                ['nullable' => false, 'default' => '0'],
                'Sort Order'
            )->addColumn(
                'created_time',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => true, 'default' => null],
                'Created Time'
            )->addColumn(
                'update_time',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => true, 'default' => null],
                'Updated Time'
            )
            ->setComment('Agorae Slider Images');
        $installer->getConnection()->createTable($table);

        $table = $installer->getConnection()
            ->newTable($installer->getTable('marketplace_mpfavouriteseller'))
            ->addColumn(
                'entity_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                'Entity ID'
            )->addColumn(
                'seller_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['unsigned' => true, 'nullable' => false, 'default' => '0'],
                'Seller Id'
            )->addColumn(
                'customer_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['unsigned' => true, 'nullable' => false, 'default' => '0'],
                'Customer_id Id'
            )->addColumn(
                'liked_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                [],
                'Liked Time'
            )->setComment('Marketplace favourite seller table');
        $installer->getConnection()->createTable($table);

        $installer->endSetup();
    }

    /**
     * MoveDirToMediaDir move directories to media
     *
     * @return void
     */
    private function moveDirToMediaDir()
    {
        try {
            $objManager = \Magento\Framework\App\ObjectManager::getInstance();
            $reader = $objManager->get(\Magento\Framework\Module\Dir\Reader::class);
            $filesystem = $objManager->get(\Magento\Framework\Filesystem::class);
            $type = \Magento\Framework\App\Filesystem\DirectoryList::MEDIA;
            $smpleFilePath = $filesystem->getDirectoryRead($type)
                                        ->getAbsolutePath().'agorae/product/';
            $files = [
                'sale' => 'sale.png',
                'latest'=>'new.png',
                'discount'=>'discount.png',
                'pick'=>'pick.png'
            ];

            if (!$this->fileDriver->isExists($smpleFilePath)) {
                $this->fileDriver->createDirectory($smpleFilePath, 0777, true);
            }

            foreach ($files as $folder => $file) {
                if (!$this->fileDriver->isExists($smpleFilePath.$folder.'/')) {
                    $this->fileDriver->createDirectory($smpleFilePath.$folder.'/', 0777, true);
                }
                $filePath = $smpleFilePath.$folder.'/'.$file;
                if (!$this->fileDriver->isExists($filePath)) {
                    $path = '/pub/media/agorae/product/'.$file;
                    $mediaFile = $reader->getModuleDir('', 'Webkul_Agorae').$path;
                    if ($this->fileDriver->isExists($mediaFile)) {
                        $this->fileDriver->copy($mediaFile, $filePath);
                    }
                }
            }
        } catch (\Exception $e) {
            $this->logger->critical('Error message', ['exception' => $e]);
        }
    }

    /**
     * MoveCategoryImagesToMediaDir move categpry image to media
     *
     * @return void
     */
    public function moveCategoryImagesToMediaDir()
    {
        try {
            $objManager = \Magento\Framework\App\ObjectManager::getInstance();
            $reader = $objManager->get(\Magento\Framework\Module\Dir\Reader::class);
            $filesystem = $objManager->get(\Magento\Framework\Filesystem::class);
            $type = \Magento\Framework\App\Filesystem\DirectoryList::MEDIA;
            $smpleFilePath = $filesystem->getDirectoryRead($type)
                                        ->getAbsolutePath().'agorae/category/';
            $file = 'slider-avatar.jpg';

            if (!$this->fileDriver->isExists($smpleFilePath)) {
                $this->fileDriver->createDirectory($smpleFilePath, 0777, true);
            }

            if (!$this->fileDriver->isExists($smpleFilePath.'/')) {
                $this->fileDriver->createDirectory($smpleFilePath.'/', 0777, true);
            }
            $filePath = $smpleFilePath.'/'.$file;
            if (!$this->fileDriver->isExists($filePath)) {
                $path = '/pub/media/agorae/category/'.$file;
                $mediaFile = $reader->getModuleDir('', 'Webkul_Agorae').$path;
                if ($this->fileDriver->isExists($mediaFile)) {
                    $this->fileDriver->copy($mediaFile, $filePath);
                }
            }
        } catch (\Exception $e) {
            $this->logger->critical('Error message', ['exception' => $e]);
        }
    }
}
