<?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;
class UpgradeData implements UpgradeDataInterface
{
    protected $customerSetupFactory;

    private $attributeSetFactory;

    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }
    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'=>'Datenschutz - Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor.',
                'custom_richtigkeit'=>'Richtigkeit der Daten - voluptua. At vero eos et accusam et justo duo dolores et ea rebum.',
            ];
            $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();
            }
        }
        $setup->endSetup();
    }
}
