<?php


namespace Magetop\Advancepreview\Setup;

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
class UpgradeData implements UpgradeDataInterface
{
    protected $customerSetupFactory;

    private $attributeSetFactory;
    private $eavSetupFactory;
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
        $this->eavSetupFactory = $eavSetupFactory;
    }
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        if (version_compare($context->getVersion(), '2.5.1') < 0) {
            $customerAttributes = [
                'custom_industry'=>'Industry / addition',
                'custom_street'=>'Street',
                'custom_number'=>'number',
                'custom_postcode'=>'Postcode',
                'custom_city'=>'City',
                'custom_country'=>'Country',
                'custom_phone'=>'Phone',
                'custom_preferred_language'=>'Preferred Language',
                'custom_sales_tax'=>'Sales tax / VAT number (only for companies)',
            ];
            $position = 100;
            foreach ($customerAttributes as $key => $customerAttribute) {
                $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    $key,
                    [
                        'type' => 'text',
                        'label' => $customerAttribute,
                        'input' => 'text',
                        'required' => false,
                        'visible' => true,
                        'user_defined' => true,
                        'sort_order' => $position,
                        'position' => $position,
                        'system' => 0,
                    ]
                );
                $Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $key)
                    ->addData([
                        'attribute_set_id' => 1,
                        'attribute_group_id' => 1,
                        'used_in_forms' => ['adminhtml_customer', 'customer_account_edit','adminhtml_checkout'],
                    ]);
                $position++;
                $Attribute->save();
            }
        }
        if (version_compare($context->getVersion(), '2.5.2') < 0) {
            $customerAttributes = [
                'custom_datenschutz'=>'Custom Register Rule 1',
                'custom_richtigkeit'=>'Custom Register Rule 2',
            ];
            $position = 115;
            foreach ($customerAttributes as $key => $customerAttribute) {
                $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    $key,
                    [
                        'type' => 'int',
                        'label' => $customerAttribute,
                        'input' => 'boolean',
                        'required' => false,
                        'visible' => true,
                        'user_defined' => true,
                        'sort_order' => $position,
                        'position' => $position,
                        'system' => 0,
                    ]
                );
                $Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $key)
                    ->addData([
                        'attribute_set_id' => 1,
                        'attribute_group_id' => 1,
                        'used_in_forms' => ['adminhtml_customer', 'customer_account_edit','adminhtml_checkout'],
                    ]);
                $position++;
                $Attribute->save();
            }
        }
        if (version_compare($context->getVersion(), '2.6.4') < 0) {
            $customerAttributes = [
                'custom_company_name'=>'Company name',
                'custom_is_seller'=>'Is Reseller?',
                'custom_preferred_language'=>'Preferred Language',
            ];
            $position = 120;
            foreach ($customerAttributes as $key => $customerAttribute) {
                $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    $key,
                    [
                        'type' => 'text',
                        'label' => $customerAttribute,
                        'input' => 'text',
                        'required' => false,
                        'visible' => true,
                        'user_defined' => true,
                        'sort_order' => $position,
                        'position' => $position,
                        'system' => 0,
                    ]
                );
                $Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $key)
                    ->addData([
                        'attribute_set_id' => 1,
                        'attribute_group_id' => 1,
                        'used_in_forms' => ['adminhtml_customer', 'customer_account_edit','adminhtml_checkout'],
                    ]);
                $position++;
                $Attribute->save();
            }
        }
        if (version_compare($context->getVersion(), '2.6.5') < 0) {
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            $eavSetup->removeAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'custom_sales_tax'
            );
        }
        if (version_compare($context->getVersion(), '2.6.6') < 0) {
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            $eavSetup->removeAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'custom_is_seller'
            );
        }
        if (version_compare($context->getVersion(), '2.6.7') < 0) {
            $customerAttributes = [
                'custom_is_reseller'=>'Is Reseller?',
            ];
            $position = 125;
            foreach ($customerAttributes as $key => $customerAttribute) {
                $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    $key,
                    [
                        'type' => 'int',
                        'label' => $customerAttribute,
                        'input' => 'boolean',
                        'required' => false,
                        'visible' => true,
                        'user_defined' => true,
                        'sort_order' => $position,
                        'position' => $position,
                        'system' => 0,
                    ]
                );
                $Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $key)
                    ->addData([
                        'attribute_set_id' => 1,
                        'attribute_group_id' => 1,
                        'used_in_forms' => ['adminhtml_customer', 'customer_account_edit','adminhtml_checkout'],
                    ]);
                $position++;
                $Attribute->save();
            }
        }

        if (version_compare($context->getVersion(), '2.8.6') < 0) {
            $customerAttributes = [
                'white_label_shop'=>'White label shop',
            ];
            $position = 130;
            foreach ($customerAttributes as $key => $customerAttribute) {
                $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    $key,
                    [
                        'type' => 'int',
                        'label' => $customerAttribute,
                        'input' => 'boolean',
                        'required' => false,
                        'visible' => true,
                        'user_defined' => true,
                        'sort_order' => $position,
                        'position' => $position,
                        'system' => 0,
                    ]
                );
                $Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $key)
                    ->addData([
                        'attribute_set_id' => 1,
                        'attribute_group_id' => 1,
                        'used_in_forms' => ['adminhtml_customer', 'customer_account_edit','adminhtml_checkout'],
                    ]);
                $position++;
                $Attribute->save();
            }
        }
        if (version_compare($context->getVersion(), '2.8.7') < 0) {
            $customerAttributes = [
                'payment_term'=>'Payment term',
            ];
            $position = 131;
            foreach ($customerAttributes as $key => $customerAttribute) {
                $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    $key,
                    [
                        'type' => 'text',
                        'label' => $customerAttribute,
                        'input' => 'select',
                        'required' => true,
                        'visible' => true,
                        'source' => \Magetop\Advancepreview\Model\Options::class,
                        'user_defined' => true,
                        'sort_order' => $position,
                        'position' => $position,
                        'system' => 0,
                    ]
                );
                $Attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $key)
                    ->addData([
                        'attribute_set_id' => 1,
                        'attribute_group_id' => 1,
                        'used_in_forms' => ['adminhtml_customer'],
                    ]);
                $position++;
                $Attribute->save();
            }
        }
        $setup->endSetup();
    }
}
