<?php

namespace Database\Seeders;

use App\Classes\Modules\Addresses\DataTransferObjects\AddressObject;
use App\Classes\Modules\Addresses\Processors\CreateAddressFromOldAddressProcessor;
use App\Classes\Modules\Addresses\Services\CreatesAddress;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyConnectionObject;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyObject;
use App\Classes\Modules\Companies\Processors\CreateCompanyModuleProcessor;
use App\Classes\Modules\Companies\Processors\CreateCompanyProcessor;
use App\Classes\Modules\Companies\Services\ApprovesCompanyConnection;
use App\Classes\Modules\Companies\Services\CreatesCompany;
use App\Classes\Modules\Companies\Services\CreatesCompanyConnection;
use App\Classes\Modules\Companies\Services\UpdatesCompanyStatus;
use App\Classes\Modules\Contacts\DataTransferObjects\ContactObject;
use App\Classes\Modules\Contacts\Processors\CreateContactProcessor;
use App\Classes\Modules\Contacts\Services\CreatesContact;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\CompanyType;
use App\Classes\ValueObjects\Constants\BusinessType;

use App\Models\CompanyModule;
use App\Models\OldCompany;
use Illuminate\Database\Seeder;

use App\Models\Company;

class CompaniesTableSeeder extends Seeder
{

    /** @var CreateCompanyProcessor */
    private $createCompanyProcessor;

    /** @var UpdatesCompanyStatus*/
    private $updatesCompanyStatus;

    /** @var CreateCompanyModuleProcessor */
    private $createCompanyModuleProcessor;

    /** @var CreateContactProcessor */
    private $createContactProcessor;

    /** @var CreatesCompanyConnection */
    private $createsCompanyConnection;

    /** @var ApprovesCompanyConnection */
    private $approvesCompanyConnection;

    /** @var CreateAddressFromOldAddressProcessor */
    private $createAddressFromOldAddressProcessor;

    /** @var CreatesAddress */
    private $createsAddress;

    /** @var CreatesCompany */
    private $createsCompany;

    /** @var CreatesContact */
    private $createsContact;

    /**
     * CompaniesTableSeeder constructor.
     * @param CreateCompanyProcessor $createCompanyProcessor
     * @param UpdatesCompanyStatus $updatesCompanyStatus
     * @param CreateCompanyModuleProcessor $createCompanyModuleProcessor
     * @param CreateContactProcessor $createContactProcessor
     * @param CreatesCompanyConnection $createsCompanyConnection
     * @param ApprovesCompanyConnection $approvesCompanyConnection
     * @param CreateAddressFromOldAddressProcessor $createAddressFromOldAddressProcessor
     * @param CreatesAddress $createsAddress
     * @param CreatesCompany $createsCompany
     * @param CreatesContact $createsContact
     */
    public function __construct(CreateCompanyProcessor $createCompanyProcessor, UpdatesCompanyStatus $updatesCompanyStatus, CreateCompanyModuleProcessor $createCompanyModuleProcessor, CreateContactProcessor $createContactProcessor, CreatesCompanyConnection $createsCompanyConnection, ApprovesCompanyConnection $approvesCompanyConnection, CreateAddressFromOldAddressProcessor $createAddressFromOldAddressProcessor, CreatesAddress $createsAddress, CreatesCompany $createsCompany, CreatesContact $createsContact)
    {
        $this->createCompanyProcessor = $createCompanyProcessor;
        $this->updatesCompanyStatus = $updatesCompanyStatus;
        $this->createCompanyModuleProcessor = $createCompanyModuleProcessor;
        $this->createContactProcessor = $createContactProcessor;
        $this->createsCompanyConnection = $createsCompanyConnection;
        $this->approvesCompanyConnection = $approvesCompanyConnection;
        $this->createAddressFromOldAddressProcessor = $createAddressFromOldAddressProcessor;
        $this->createsAddress = $createsAddress;
        $this->createsCompany = $createsCompany;
        $this->createsContact = $createsContact;
    }


    /**
     * Run the database seeds.
     *
     * @return void
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function run()
    {
        $companies = [
            new CompanyObject('CIEF Worldwide Sdn Bhd', 'CIEF', CompanyType::COMPANY_BUSINESS, ApprovalStatus::APPROVED),
            new CompanyObject('Atvantic Import & Export Sdn Bhd', 'VT-NATION', CompanyType::COMPANY_BUSINESS, ApprovalStatus::APPROVED)
        ];


        foreach ($companies as $companyObject) {

            /** @var Company $company */
            $company = $this->createsCompany->execute($companyObject);

            if($companyObject->getReference() === 'CIEF') {
                /** @var CompanyModule $companyModule */
                $companyModule = $this->createCompanyModuleProcessor->execute($company, BusinessType::FREIGHT_FORWARDER);

                $address = new AddressObject('Malaysian Global Innovation & Creativity Center Level 1 CWS', 'Block 3730,Persiaran APEC', 1, 15, 402, 63000);
                $this->createsAddress->execute($companyModule, $address);

                $contact = new ContactObject('Office Number', '0182909252', '', '');
                $this->createsContact->execute($companyModule, $contact);

                continue;
            }

            foreach ([BusinessType::FREIGHT_FORWARDER, BusinessType::WAREHOUSE] as $businessType){

                if($businessType === BusinessType::WAREHOUSE){
                    foreach(['Guangzhou', 'Yiwu', 'Klang', 'Sabah', 'Sarawak'] as $name){

                        /** @var CompanyModule $companyModule */
                        $companyModule = $this->createCompanyModuleProcessor->execute($company, $businessType);

                        if ($name === 'Guangzhou'){
                            $companyModule->update(['name' => $name, 'reference' => 'GZ-V01']);
                            $address = new AddressObject('白云区，大朗南路 白云湖大道自编13号 7号仓库', '高德地图导航：广州市世通行', 2, 35, 633, 510450);
                            $contact = new ContactObject('杨维民', '13600055019', '', '');
                        } elseif ($name === 'Yiwu') {
                            $companyModule->update(['name' => $name, 'reference' => 'YW-V01']);
                            $address = new AddressObject('义乌市诚信大道义乌港A区2楼194号', '', 2, 27, 532, 322000);
                            $contact = new ContactObject('小曾', '13575982901', '', '');
                        } elseif ($name === 'Klang') {
                            $companyModule->update(['name' => $name, 'reference' => 'KL-V01']);
                            $address = new AddressObject('LOT 1357 A, JALAN TANJUNG SHAWAL', 'KAMPUNG SUNGAI PINANG', 1, 15, 412, 41400);
                            $contact = new ContactObject('Mr. Yang', '0162237680', '', '');
                        } elseif ($name === 'Sabah') {
                            $companyModule->update(['name' => $name, 'reference' => 'SB-V01']);
                            $address = new AddressObject('Warehouse A & B, No. 2, Lorong Kacang Tanah 1', 'Jalan Kolombong, Kolombong', 1, 13, 316, 88450);
                        } elseif ($name === 'Sarawak') {
                            $companyModule->update(['name' => $name, 'reference' => 'SRW-V01']);
                            $address = new AddressObject('OPPOSITE HT POLE 51, LT 664 JALAN SUNGAI SALIM', '', 1, 14, 381, 96000);
                        }


                        $this->createsAddress->execute($companyModule, $address);
                        $this->createsContact->execute($companyModule, $contact);

                    }

                    continue;
                }

                /** @var CompanyModule $companyModule */
                $companyModule = $this->createCompanyModuleProcessor->execute($company, $businessType);

                $address = new AddressObject('2, Jalan TSB 6, Taman Industri Sungai Buloh', '', 1, 15, 419, 47000);
                $this->createsAddress->execute($companyModule, $address);

                $contact = new ContactObject('Office Number', '036148 8108', '', '');
                $this->createsContact->execute($companyModule, $contact);

            }


        }

//         if(true){
//             $companies = OldCompany::all();
//
//             foreach($companies as $company){
//
//                 $marking = explode("CIEF/", $company->marking);
//
//                 if(count($marking) < 2){
//                     continue;
//                 }
//
//                 $marking = str_replace(' ', '', str_replace('/', '', $marking[1]));
//
//
//                 /** @var Company $newCompany */
//                 $newCompany = $this->createCompanyProcessor->execute($company->name, CompanyType::COMPANY_BUSINESS, ApprovalStatus::PENDING_SUBMISSION);
//
//                 $this->updatesCompanyStatus->execute($newCompany, ApprovalStatus::APPROVED);
//
//                 /** @var CompanyModule $companyModule */
//                 $companyModule = $this->createCompanyModuleProcessor->execute($newCompany, BusinessType::IMPORTER);
//
//                 $connectionObject = new CompanyConnectionObject($companyModule, 'CIEF', $marking);
//
//                 $connection = $this->createsCompanyConnection->execute($connectionObject);
//                 $this->approvesCompanyConnection->execute($connection);
//
//                 $i = 0;
//                 foreach($company->addresses as $address){
//                     $this->createAddressFromOldAddressProcessor->execute($address, $companyModule);
//                     $contact = preg_replace("/[^0-9.]/", "", $address->contact);
//                     if($contact){
//                         $i++;
//                         if($i === 1) {
//                             $contactObject = new ContactObject('', $contact, $company->email, '',);
//                             $this->createContactProcessor->execute($contactObject, $newCompany);
//                         }
//                     }
//
//
//                 }
//             }
//         }


    }

}
