<?php

namespace Planaday\Platform\WordpressConfig\Migrations;

use Planaday\Config\OptionGroups\Booking\BookingOptions;
use Planaday\Config\OptionGroups\Booking\BookingOptionsEnum;
use Planaday\Config\OptionGroups\Booking\Form\Address\AddressOptionsEnum;
use Planaday\Config\OptionGroups\Booking\Form\BookingFormOptionsEnum;
use Planaday\Config\OptionGroups\Booking\Form\Company\CompanyOptionsEnum;
use Planaday\Config\OptionGroups\Booking\Form\Company\Invoice\InvoiceOptionsEnum;
use Planaday\Config\OptionGroups\Booking\Form\Materials\MaterialsOptionsEnum;
use Planaday\Config\OptionGroups\Booking\Form\Student\StudentOptionsEnum;
use Planaday\Config\OptionGroups\Booking\Submission\BookingSubmissionOptionsEnum;
use Planaday\Config\OptionGroups\Booking\Submission\Mail\MailOptionsEnum;
use Planaday\Config\OptionGroups\Booking\Submission\Redirecting\RedirectOptionsEnum;
use Planaday\Config\OptionGroups\Course\Calendar\CalendarOptionsEnum;
use Planaday\Config\OptionGroups\Course\CourseOptions;
use Planaday\Config\OptionGroups\Course\CourseOptionsEnum;
use Planaday\Config\OptionGroups\Course\Detail\DetailOptionsEnum;
use Planaday\Config\OptionGroups\Course\Overview\OverviewOptionsEnum;
use Planaday\Config\OptionGroups\Course\Search\SearchOptionsEnum;
use Planaday\Config\OptionGroups\General\Api\ApiOptionsEnum;
use Planaday\Config\OptionGroups\General\Debug\DebugOptionsEnum;
use Planaday\Config\OptionGroups\General\GeneralOptions;
use Planaday\Config\OptionGroups\General\GeneralOptionsEnum;
use Planaday\Config\OptionGroups\General\Price\PriceOptionsEnum;
use Planaday\Config\OptionGroups\Internal\InternalOptions;
use Planaday\Config\OptionGroups\Internal\InternalOptionsEnum;
use Planaday\Config\OptionGroups\Payment\PaymentOptions;
use Planaday\Config\OptionGroups\Payment\PaymentOptionsEnum;

class MigrateTo1_0_0 {
	public static function migrate( MigrationHelper $helper ): void{
		$oldOptions = get_option( 'planaday-api-general', null);

		if(!isset($oldOptions) || !is_array($oldOptions)) {
			update_option( $helper->pluginData->getFullPrefix() . '-version', '1.0.0' );
			return;
		}

		$bookingOptions = [
			BookingOptionsEnum::BOOKING_FORM => [
				BookingFormOptionsEnum::ADDRESS => [
					AddressOptionsEnum::ASK_FOR_NUMBER_EXTENSION => $helper->getBool( $oldOptions['vraaghuisnrext'] ?? null )
				],

				BookingFormOptionsEnum::BOOKING_FORM_TITLE => $helper->getString( $oldOptions['teksttitelbooking'] ?? null ),

				BookingFormOptionsEnum::COMPANY => [
					CompanyOptionsEnum::ALLOW_COMPANIES       => ! $helper->getBool( $oldOptions['onlybookingelparticulier'] ?? null ),
					CompanyOptionsEnum::AS_COMPANY_DEFAULT    => ! $helper->getBool( $oldOptions['voorkeurbooleancompany'] ?? null ),
					CompanyOptionsEnum::ASK_PHONE_NUMBER      => $helper->getBool( $oldOptions['toonformphonenumbercompany'] ?? null ),
					CompanyOptionsEnum::PHONE_NUMBER_REQUIRED => $helper->getBool( $oldOptions['toonformphonenumbercompanymanatory'] ?? null ),

					CompanyOptionsEnum::INVOICE => [
						InvoiceOptionsEnum::ASK_FOR_ADDRESS => $helper->getBool( $oldOptions['vraagfinancieleinfobijbedrijf'] ?? null ),
						InvoiceOptionsEnum::ASK_FOR_EMAIL   => $helper->getBool( $oldOptions['toonemailinvoice'] ?? null ),
						InvoiceOptionsEnum::EMAIL_REQUIRED  => $helper->getBool( $oldOptions['toonemailinvoicemanatory'] ?? null )
					]
				],

				BookingFormOptionsEnum::MATERIALS => [
					MaterialsOptionsEnum::ASK_MATERIALS_BOOKING_FORM           => $helper->getBool( $oldOptions['materialbookingactive'] ?? null ),
					MaterialsOptionsEnum::MATERIALS_FORM_DESCRIPTION           => $helper->getString( $oldOptions['materialtext'] ?? null ),
					MaterialsOptionsEnum::MATERIALS_FORM_TITLE                 => $helper->getString( $oldOptions['materialtitle'] ?? null )
				],

				BookingFormOptionsEnum::STUDENT => [
					StudentOptionsEnum::ASK_ADDRESS            => $helper->getBool( $oldOptions['vraagadrescursist'] ?? null ),
					StudentOptionsEnum::ASK_IF_CONTACT         => $helper->getBool( $oldOptions['vraagcontactpersoon'] ?? null ),
					StudentOptionsEnum::ASK_NICK_NAME          => $helper->getBool( $oldOptions['vraagroepnaam'] ?? null ),
					StudentOptionsEnum::ASK_MAIDEN_NAME        => $helper->getBool( $oldOptions['vraagmeisjesnaam'] ?? null ),
					StudentOptionsEnum::ASK_INTERNAL_REFERENCE => $helper->getBool( $oldOptions['vraag_internal_reference'] ?? null ),
					StudentOptionsEnum::ASK_COMPANY_POSITION   => $helper->getBool( $oldOptions['toonformpositionstudent'] ?? null ),
					StudentOptionsEnum::ASK_COST_CENTER_CODE   => $helper->getBool( $oldOptions['vraagcostcentercode'] ?? null ),
					StudentOptionsEnum::ASK_EMPLOYEE_NUMBER    => $helper->getBool( $oldOptions['vraagpersoneelsnummer'] ?? null ),
					StudentOptionsEnum::ASK_DATE_OF_BIRTH      => $helper->getBool( $oldOptions['toonformdateofbirth'] ?? null ),
					StudentOptionsEnum::ASK_PLACE_OF_BIRTH     => $helper->getBool( $oldOptions['toonformcountryofbirth'] ?? null ),
					StudentOptionsEnum::ASK_PHONE_NUMBER       => $helper->getBool( $oldOptions['toonformphonenumber'] ?? null ),

					StudentOptionsEnum::ASK_CODE_95 => $helper->getBool( $oldOptions['toonoptiecode95'] ?? null ),
					StudentOptionsEnum::ASK_SOOB    => $helper->getBool( $oldOptions['toonoptiesoob'] ?? null ),

					StudentOptionsEnum::ASK_REMARK => $helper->getBool( $oldOptions['toonstudentremark'] ?? null ),

					StudentOptionsEnum::COST_CENTER_CODE_REQUIRED => $helper->getBool( $oldOptions['vraagcostcentercodemanatory'] ?? null ),
					StudentOptionsEnum::DATE_OF_BIRTH_REQUIRED    => $helper->getBool( $oldOptions['toonformdateofbirthmanatory'] ?? null ),
					StudentOptionsEnum::PLACE_OF_BIRTH_REQUIRED   => $helper->getBool( $oldOptions['toonformcountryofbirthmanatory'] ?? null ),
					StudentOptionsEnum::PHONE_NUMBER_REQUIRED     => $helper->getBool( $oldOptions['toonformphonenumbermanatory'] ?? null ),
					StudentOptionsEnum::COMPANY_POSITION_REQUIRED => $helper->getBool( $oldOptions['toonformpositionstudentmanatory'] ?? null ),

					StudentOptionsEnum::TERMS_AND_CONDITIONS_REQUIRED => $helper->getBool( $oldOptions['toonformalgemenevoorwaarden'] ?? null ),
					StudentOptionsEnum::TERMS_AND_CONDITIONS_URL      => $helper->getString( $oldOptions['urlalgemenevoorwaarden'] ?? null ),

					StudentOptionsEnum::COST_CENTER_CODE_TEXT => $helper->getString( $oldOptions['tekstkostenplaats'] ?? null ),
					StudentOptionsEnum::CODE_95_TEXT          => $helper->getString( $oldOptions['tekstcode95optie'] ?? null ),
					StudentOptionsEnum::SOOB_TEXT             => $helper->getString( $oldOptions['tekstsooboptie'] ?? null )
				],

				BookingFormOptionsEnum::ASK_API_ATTRIBUTES => $helper->getBool( $oldOptions['toonapiattributen'] ?? null )
			],
			BookingOptionsEnum::SUBMISSION   => [
				BookingSubmissionOptionsEnum::MAIL => [
					MailOptionsEnum::MAIL_STUDENT          => $helper->getBool( $oldOptions['mailcursusaanmeldingcursist'] ?? null ),
					MailOptionsEnum::MAIL_COMPANY          => $helper->getBool( $oldOptions['mailcursusaanmeldingbedrijf'] ?? null ),
					MailOptionsEnum::MAIL_STUDENT_CONTENTS => $helper->getString( $oldOptions['mailbedankttekst'] ?? null ),
					MailOptionsEnum::MAIL_COMPANY_CONTENTS => $helper->getString( $oldOptions['mailbedankttekstbedrijf'] ?? null ),
					MailOptionsEnum::MAIL_ADMIN            => $helper->checkIfEmail( $oldOptions['mailcursusaanmelding'] ?? null ),
					MailOptionsEnum::ADMIN_ADDRESS         => $helper->getString( $oldOptions['mailcursusaanmelding'] ?? null ),
				],

				BookingSubmissionOptionsEnum::REDIRECT => [
					RedirectOptionsEnum::REDIRECT_AFTER_SUCCESS   => $helper->getBool( $oldOptions['bedanktredirect'] ?? null ),
					RedirectOptionsEnum::REDIRECT_SUCCESS_PAGE    => $helper->getInt( $oldOptions['bedankurl'] ?? null ),
					RedirectOptionsEnum::NO_REDIRECT_SUCCESS_TEXT => $helper->getString( $oldOptions['bedankttekst'] ?? null ),

					RedirectOptionsEnum::REDIRECT_AFTER_FAILURE   => $helper->getBool( $oldOptions['bedanktredirectmislukt'] ?? null ),
					RedirectOptionsEnum::REDIRECT_FAILURE_PAGE    => $helper->getInt( $oldOptions['bedankurlmislukt'] ?? null ),
					RedirectOptionsEnum::NO_REDIRECT_FAILURE_TEXT => $helper->getString( $oldOptions['bedankttekstmislukt'] ?? null ),
				]
			]
		];
		update_option( $helper->pluginData->getFullPrefix() . BookingOptions::INDEX, $bookingOptions );

		$courseOptions = [
			CourseOptionsEnum::CALENDAR_SETTINGS => [
				CalendarOptionsEnum::BACKGROUND_COLOR    => $helper->getString( $oldOptions['calendarcolorback'] ?? null ),
				CalendarOptionsEnum::TEXT_COLOR          => $helper->getString( $oldOptions['calendarcolortext'] ?? null ),
				CalendarOptionsEnum::SHOW_MOUSEOVER_TEXT => $helper->getBool( $oldOptions['tooncalendarmouseover'] ?? null )
			],

			CourseOptionsEnum::DETAIL_SETTINGS => [
				DetailOptionsEnum::SHOW_SOOB_LABEL                 => $helper->getBool( $oldOptions['toonsoobdetailcursus'] ?? null ),
				DetailOptionsEnum::SHOW_CODE_95_LABEL              => $helper->getBool( $oldOptions['tooncode95cursus'] ?? null ),
				DetailOptionsEnum::SHOW_LABELS                     => $helper->getBool( $oldOptions['tooncursuslabelsdetail'] ?? null ),
				DetailOptionsEnum::SHOW_DAY_PART_COUNT             => $helper->getBool( $oldOptions['toondetaildagdelen'] ?? null ),
				DetailOptionsEnum::SHOW_DAY_PART_LIST              => $helper->getBool( $oldOptions['toondetaildagdelenlijst'] ?? null ),
				DetailOptionsEnum::SHOW_LOCATION_DAY_PART          => $helper->getBool( $oldOptions['toonlocatiedagdeel'] ?? null ),
				DetailOptionsEnum::SHOW_E_LEARNING_ICON            => $helper->getBool( $oldOptions['tooncursuselearning'] ?? null ),
				DetailOptionsEnum::SHOW_E_LEARNING_DAY_PART        => $helper->getBool( $oldOptions['toonelearningdagdeel'] ?? null ),
				DetailOptionsEnum::SHOW_LEVEL_COURSE               => $helper->getBool( $oldOptions['toonniveau'] ?? null ),
				DetailOptionsEnum::SHOW_DESCRIPTION_DAY_PART       => $helper->getBool( $oldOptions['toonomschrijvingdagdeel'] ?? null ),
				DetailOptionsEnum::SHOW_AVAILABILITY               => $helper->getBool( $oldOptions['toonbeschikbareplaatsen'] ?? null ),
				DetailOptionsEnum::SHOW_START_GUARANTEE_LABEL      => $helper->getBool( $oldOptions['startgarantiedetail'] ?? null ),
				DetailOptionsEnum::SHOW_MONEY_BACK_LABEL           => $helper->getBool( $oldOptions['geldterugdetail'] ?? null ),
				DetailOptionsEnum::SHOW_FINANCIAL_INFO             => $helper->getBool( $oldOptions['tooncostsremark'] ?? null ),
				DetailOptionsEnum::SHOW_PRICE                      => $helper->getBool( $oldOptions['toonprijsdetailpagina'] ?? null ),
				DetailOptionsEnum::SHOW_PRICE_WITH_AND_WITHOUT_TAX => $helper->getBool( $oldOptions['detailbothprices'] ?? null ),
				DetailOptionsEnum::SHOW_ICONS                      => $helper->getBool( $oldOptions['detailshowicons'] ?? null ),
				DetailOptionsEnum::INCLUDE_E_LEARNING_DAY_PARTS    => $helper->getBool( $oldOptions['countelearningasdaypart'] ?? null ),
				DetailOptionsEnum::SHOW_DESCRIPTION                => $helper->getBool( $oldOptions['toonomschrijvingcursus'] ?? null ),
				DetailOptionsEnum::SHOW_LABELS_DAY_PART            => $helper->getBool( $oldOptions['toonlabelindagdeel'] ?? null ),
			],

			CourseOptionsEnum::GENERAL_SETTINGS => [
				\Planaday\Config\OptionGroups\Course\General\GeneralOptionsEnum::DAY_PART_TEXT           => $helper->getString( $oldOptions['dagdelentekst'] ?? null ),
				\Planaday\Config\OptionGroups\Course\General\GeneralOptionsEnum::INCLUDE_OPTION_IN_COUNT => $helper->getBool( $oldOptions['optiesmeetellen'] ?? null ),
				\Planaday\Config\OptionGroups\Course\General\GeneralOptionsEnum::SKIP_E_LEARNING_ONLY    => $helper->getBool( $oldOptions['skipcoursewithonlyelearning'] ?? null )
			],

			CourseOptionsEnum::OVERVIEW_SETTINGS => [
				OverviewOptionsEnum::SHOW_CODE_95_ICON           => $helper->getBool( $oldOptions['tooncode95overzicht'] ?? null ),
				OverviewOptionsEnum::SHOW_SOOB_ICON              => $helper->getBool( $oldOptions['toonsooboverzicht'] ?? null ),
				OverviewOptionsEnum::SHOW_E_LEARNING_ICON        => $helper->getBool( $oldOptions['tooncursuselearning'] ?? null ),
				OverviewOptionsEnum::SHOW_MONEY_BACK_NOTICE      => $helper->getBool( $oldOptions['geldterugoverzicht'] ?? null ),
				OverviewOptionsEnum::SHOW_START_GUARANTEE_NOTICE => $helper->getBool( $oldOptions['startgarantieoverzicht'] ?? null ),
				OverviewOptionsEnum::SHOW_FINANCIAL_INFO         => $helper->getBool( $oldOptions['tooncostsremarkoverzicht'] ?? null ),
				OverviewOptionsEnum::SHOW_PRICE                  => $helper->getBool( $oldOptions['toonprijs'] ?? null ),
				OverviewOptionsEnum::SHOW_DAY_PART_COUNT         => $helper->getBool( $oldOptions['toondagdelen'] ?? null ),
				OverviewOptionsEnum::SHOW_LABELS                 => $helper->getBool( $oldOptions['tooncursuslabelsoverzicht'] ?? null ),
				OverviewOptionsEnum::SHOW_FIRST_LOCATION         => $helper->getBool( $oldOptions['toonlocatiebijoverzicht'] ?? null ),
				OverviewOptionsEnum::SHOW_AVAILABILITY           => $helper->getBool( $oldOptions['toonbeschikbareplaatsen'] ?? null ),

				OverviewOptionsEnum::SHOW_COURSE_BUTTON => $helper->getBool( $oldOptions['toonbutton'] ?? null ),
				OverviewOptionsEnum::COURSE_BUTTON_TEXT => $helper->getString( $oldOptions['buttontekstoverzicht'] ?? null ),

				OverviewOptionsEnum::SHOW_DESCRIPTION       => isset($oldOptions['tooninblockvorm']) ? ($helper->getInt( $oldOptions['limiettekstomschrijving']) !== 0) : null,
				OverviewOptionsEnum::MAX_LENGTH_DESCRIPTION => $helper->getInt( $oldOptions['limiettekstomschrijving'] ?? null ),

				OverviewOptionsEnum::SHOW_DAY_PART_LIST           => $helper->getBool( $oldOptions['toondetaildagdelenlijstinoverzicht'] ?? null ),
				OverviewOptionsEnum::INCLUDE_E_LEARNING_DAY_PARTS => $helper->getBool( $oldOptions['countelearningasdaypart'] ?? null ),
				OverviewOptionsEnum::SHOW_LOCATION_DAY_PART       => $helper->getBool( $oldOptions['toonlocatiedagdeeloverzicht'] ?? null ),
				OverviewOptionsEnum::SHOW_LABELS_DAY_PART         => $helper->getBool( $oldOptions['toonlabelindagdeel'] ?? null ),

				OverviewOptionsEnum::NO_COURSE_TEXT                   => $helper->getString( $oldOptions['tekstgeencursussen'] ?? null ),
				OverviewOptionsEnum::INCLUDE_FULL_COURSES             => $helper->getBool( $oldOptions['toonvollecursus'] ?? null ),
				OverviewOptionsEnum::INCLUDE_PARTIALLY_PASSED_COURSES => $helper->getBool( $oldOptions['tooncursusdagdelenverleden'] ?? null )
			],

			CourseOptionsEnum::SEARCH_SETTINGS => [
				SearchOptionsEnum::ASK_TITLE      => $helper->getBool( $oldOptions['toontextbijsearch'] ?? null ),
				SearchOptionsEnum::ASK_LOCATION   => $helper->getBool( $oldOptions['toonlocatiesbijsearch'] ?? null ),
				SearchOptionsEnum::ASK_LABELS     => $helper->getBool( $oldOptions['toonlabelsbijsearch'] ?? null ),
				SearchOptionsEnum::ASK_CODE_95    => $helper->getBool( $oldOptions['tooncode95bijsearch'] ?? null ),
				SearchOptionsEnum::ASK_SOOB       => $helper->getBool( $oldOptions['toonsoobbijsearch'] ?? null ),
				SearchOptionsEnum::ASK_E_LEARNING => $helper->getBool( $oldOptions['toonelearningbijsearch'] ?? null ),
				SearchOptionsEnum::OUTPUT_FORMAT  => isset($oldOptions['tooninblockvorm']) ? ($helper->getBool( $oldOptions['tooninblockvorm'] ) !== false ? 0 : 2) : null
			]
		];
		update_option( $helper->pluginData->getFullPrefix() . CourseOptions::INDEX, $courseOptions );

		$generalOptions = [
			GeneralOptionsEnum::API_SETTINGS => [
				ApiOptionsEnum::API_KEY => $helper->getString( $oldOptions['key'] ?? null ),
				ApiOptionsEnum::DOMAIN  => $helper->getString( $oldOptions['url'] ?? null ),
				ApiOptionsEnum::REFRESH_INTERVAL_COURSES   => $helper->getInt( $oldOptions['dbcoursehours'] ?? null ),
				ApiOptionsEnum::REFRESH_INTERVAL_DAY_PARTS => $helper->getInt( $oldOptions['dbdaypartshours'] ?? null ),
				ApiOptionsEnum::REFRESH_INTERVAL_LOCATIONS => $helper->getInt( $oldOptions['dbdaypartshours'] ?? null )
			],

			GeneralOptionsEnum::DEBUG => [
				DebugOptionsEnum::SEND_DEBUG_LOGS => null
			],

			GeneralOptionsEnum::PRICE_OPTIONS => [
				PriceOptionsEnum::INCLUDING_TAX  => !$helper->getBool( $oldOptions['btwinofexbtwtonen'] ?? null ),
				PriceOptionsEnum::WITH_TAX_LABEL => $helper->getBool( $oldOptions['btwinofexbtwtonenlabel'] ?? null ),
			]
		];
		update_option( $helper->pluginData->getFullPrefix() . GeneralOptions::INDEX, $generalOptions );

		$oldPaymentOptions = get_option( 'planaday-api-payment' );
		$paymentOptions = [
			PaymentOptionsEnum::PAYMENT_NOTICE => $helper->getString( $oldPaymentOptions['toelichting'] ?? null ),
			PaymentOptionsEnum::PAYMENT_ACTIVE => $helper->getBool( $oldOptions['betalingenactief'] ?? null ),
			PaymentOptionsEnum::CANCEL_BOOKING_IF_NO_PAYMENT => $helper->getBool( $oldPaymentOptions['cancelbookingafternopayment'] ?? null ),
			PaymentOptionsEnum::TEST_PAYMENT_ACTIVE => $helper->getBool( $oldPaymentOptions['idealtestmodus'] ?? null )
		];
		update_option( $helper->pluginData->getFullPrefix() . PaymentOptions::INDEX, $paymentOptions );

		$oldCss = get_option('planaday-api-css');
		$internalOptions = [
			InternalOptionsEnum::GLOBAL_CSS => $helper->getString( $oldCss['sccss-content'] )
		];
		update_option( $helper->pluginData->getFullPrefix() . InternalOptions::INDEX, $internalOptions );

		update_option( $helper->pluginData->getFullPrefix() . '-version', '1.0.0' );
	}
}