<?php 

namespace Lcmigrator\LcMigrator\ImporterV2;
use Lcmigrator\LcMigrator\Logger\Logger;

class ImportDone {
    public static function execute( $params ) {
		global $wp_rewrite;

		

        // Check package.json file
        if ( is_file( lc_migrator_get_package_path( $params ) ) ) {

            // Read package.json file
            $handle = fopen( lc_migrator_get_package_path( $params ), 'r' );

            // Parse package.json file
            $package = fread( $handle, filesize( lc_migrator_get_package_path( $params ) ) );
            $package = json_decode( $package, true );

            // Close handle
            fclose( $handle );

            // Activate WordPress plugins
            if ( isset( $package['Plugins'] ) && ( $plugins = $package['Plugins'] ) ) {
                lc_migrator_activate_plugins( $plugins );
            }

            // Activate WordPress template
            if ( isset( $package['Template'] ) && ( $template = $package['Template'] ) ) {
                lc_migrator_activate_template( $template );
            }

            // Activate WordPress stylesheet
            if ( isset( $package['Stylesheet'] ) && ( $stylesheet = $package['Stylesheet'] ) ) {
                lc_migrator_activate_stylesheet( $stylesheet );
            }

            // Deactivate WordPress SSL plugins
            if ( ! is_ssl() ) {
                lc_migrator_deactivate_plugins(
                    array(
                        lc_migrator_discover_plugin_basename( 'really-simple-ssl/rlrsssl-really-simple-ssl.php' ),
                        lc_migrator_discover_plugin_basename( 'wordpress-https/wordpress-https.php' ),
                        lc_migrator_discover_plugin_basename( 'wp-force-ssl/wp-force-ssl.php' ),
                        lc_migrator_discover_plugin_basename( 'force-https-littlebizzy/force-https.php' ),
                    )
                );

                lc_migrator_woocommerce_force_ssl( false );
            }

            // Deactivate WordPress plugins
            lc_migrator_deactivate_plugins(
                array(
                    lc_migrator_discover_plugin_basename( 'invisible-recaptcha/invisible-recaptcha.php' ),
                    lc_migrator_discover_plugin_basename( 'wps-hide-login/wps-hide-login.php' ),
                    lc_migrator_discover_plugin_basename( 'hide-my-wp/index.php' ),
                    lc_migrator_discover_plugin_basename( 'hide-my-wordpress/index.php' ),
                    lc_migrator_discover_plugin_basename( 'mycustomwidget/my_custom_widget.php' ),
                    lc_migrator_discover_plugin_basename( 'lockdown-wp-admin/lockdown-wp-admin.php' ),
                    lc_migrator_discover_plugin_basename( 'rename-wp-login/rename-wp-login.php' ),
                    lc_migrator_discover_plugin_basename( 'wp-simple-firewall/icwp-wpsf.php' ),
                    lc_migrator_discover_plugin_basename( 'join-my-multisite/joinmymultisite.php' ),
                    lc_migrator_discover_plugin_basename( 'multisite-clone-duplicator/multisite-clone-duplicator.php' ),
                    lc_migrator_discover_plugin_basename( 'wordpress-mu-domain-mapping/domain_mapping.php' ),
                    lc_migrator_discover_plugin_basename( 'wordpress-starter/siteground-wizard.php' ),
                    lc_migrator_discover_plugin_basename( 'pro-sites/pro-sites.php' ),
                    lc_migrator_discover_plugin_basename( 'wpide/WPide.php' ),
                    lc_migrator_discover_plugin_basename( 'page-optimize/page-optimize.php' ),
                    lc_migrator_discover_plugin_basename( 'update-services/update-services.php' ),
                )
            );
            
            // Deactivate Jetpack modules
            lc_migrator_deactivate_jetpack_modules( array( 'photon', 'sso' ) );

            // Flush Elementor cache
            lc_migrator_elementor_cache_flush();

            // Initial DB version
            lc_migrator_initial_db_version();
        }
    


		// Clear auth cookie (WP Cerber)
		if ( lc_migrator_validate_plugin_basename( 'wp-cerber/wp-cerber.php' ) ) {
			wp_clear_auth_cookie();
		}

		$should_reset_permalinks = false;

		// Switch to default permalink structure
		if ( ( $should_reset_permalinks = lc_migrator_should_reset_permalinks( $params ) ) ) {
			$wp_rewrite->set_permalink_structure( '' );
		}


		return $params;
	}
}