<?php
/**
 * @copyright	Copyright (c) 2016 AmyTheme (http://www.amytheme.com). All rights reserved.
 * @license		http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
 */

defined( 'ABSPATH' ) or die;

// get sample data file
if ( ! function_exists( 'amy_movie_demo_importer_get_data_file' ) ) {
	function amy_movie_demo_importer_get_data_file( $file, $id ) {
		$file	= esc_url( 'http://resources.amytheme.com/demo/movie/' . sanitize_title_with_dashes( $id ) . '.json' );

		return $file;
	}

	add_filter( 'amy_demo_importer_get_data_file', 'amy_movie_demo_importer_get_data_file', 10, 2 );
}

// save mc4wp settings.
if ( ! function_exists( 'amy_movie_demo_importer_save_mc4wp_settings' ) ) {
	function amy_movie_demo_importer_save_mc4wp_settings( $current_settings ) {
		$current_settings['mc4wp_default_form_id']	= get_option( 'mc4wp_default_form_id' );

		return $current_settings;
	}

	add_filter( 'amy_demo_importer_save_current_settings', 'amy_movie_demo_importer_save_mc4wp_settings' );
}

// restore mc4wp settings.
if ( ! function_exists( 'amy_movie_demo_importer_restore_mc4wp_settings' ) ) {
	function amy_movie_demo_importer_restore_mc4wp_settings( $history ) {
		update_option( 'mc4wp_default_form_id', $history['mc4wp_default_form_id'] );
	}

	add_action( 'amy_demo_importer_restore_settings', 'amy_movie_demo_importer_restore_mc4wp_settings' );
}

// remap mc4wp settings
if ( ! function_exists( 'amy_movie_demo_importer_remap_mc4wp_settings' ) ) {
	function amy_movie_demo_importer_remap_mc4wp_settings( $data ) {
		if ( isset( $data->mc4wp_default_form_id ) ) {
			$post_ids	= Amy_Demo_Importer_Map::instance()->get( 'posts' );

			if ( isset( $post_ids[ $data->mc4wp_default_form_id ] ) ) {
				update_option( 'mc4wp_default_form_id', $post_ids[ $data->mc4wp_default_form_id ][0] );
			}
		}
	}

	add_action( 'amy_demo_importer_remap_settings', 'amy_movie_demo_importer_remap_mc4wp_settings' );
}

// add revolution slider
if ( ! function_exists( 'amy_movie_demo_importer_add_revslider_sliders' ) ) {
	function amy_movie_demo_importer_add_revslider_sliders( $id ) {
		if ( class_exists( 'RevSlider' ) ) {
			global $wp_filesystem;

			$base_url	= esc_url( 'http://resources.amytheme.com/demo/movie/' );
			$sliders	= array( 'home-slider.zip' );
			$theme		= apply_filters( 'amy_demo_importer_get_theme_name', get_template() );
			$map		= Amy_Demo_Importer_Map::instance();

			foreach ( $sliders as $slider ) {
				$file		= $base_url . $slider;
				$local_path	= get_home_path() . '/wp-content/uploads/' . $theme . '/' . sanitize_title_with_dashes( $id ) . '/' . $slider;

				// check if file was downloaded
				if ( file_exists( $local_path ) ) {
					// compare file size
					$response   = wp_remote_head( $file );
					$size		= $response['headers']['content-length'];

					if ( $size != filesize( $local_path ) ) {
						// download file
						$response	= wp_remote_get( $file, array( 'timeout' => 1200 ) );
						$content	= $response['body'];

						$wp_filesystem->put_contents( $local_path, $content, 'w' );
					}
				} else {
					wp_mkdir_p( dirname( $local_path ) );

					// download file
					$response	= wp_remote_get( $file, array( 'timeout' => 1200 ) );
					$content	= $response['body'];

					$wp_filesystem->put_contents( $local_path, $content, 'w' );
				}

				$revslider	= new RevSlider();
				$result		= $revslider->importSliderFromPost( true, true, $local_path, false, false, true );

				if ( isset( $result['success'] ) && $result['success'] ) {
					$map->save( 'revslider', $result['sliderID'], $result['sliderID'], null );
				}
			}
		}
	}

	add_action( 'amy_demo_importer_finish_install', 'amy_movie_demo_importer_add_revslider_sliders' );
}

if ( ! function_exists( 'amy_movie_demo_importer_remove_revslider_sliders' ) ) {
	function amy_movie_demo_importer_remove_revslider_sliders() {
		if ( class_exists( 'RevSlider' ) ) {
			$sliders	= Amy_Demo_Importer_Map::instance()->get( 'revslider' );

			foreach ( $sliders as $slider ) {
				$revslider	= new RevSlider();

				$revslider->initByID( $slider );
				$revslider->deleteSlider();
			}
		}
	}

	add_action( 'amy_demo_importer_finish_uninstall', 'amy_movie_demo_importer_remove_revslider_sliders' );
}

if ( ! function_exists( 'amy_movie_demo_importer_remap_page_theme_options' ) ) {
	function amy_movie_demo_importer_remap_page_theme_options( $post, $url ) {
		$post_ids	= Amy_Demo_Importer_Map::instance()->get( 'posts' );

		if ( isset( $post_ids[ $post->id ] ) && isset( $post->meta->_amy_framework_options ) ) {
			$theme_options	= unserialize( $post->meta->_amy_framework_options );

			if ( isset( $theme_options['options'] ) ) {
				$theme_options['options']	= amy_encode_string( amy_movie_demo_importer_remap_theme_options( amy_decode_string( $theme_options['options'] ), $url ) );

				update_post_meta( $post_ids[ $post->id ][0], '_amy_framework_options', $theme_options );
			}
		}
	}

	add_action( 'amy_demo_importer_remap_post', 'amy_movie_demo_importer_remap_page_theme_options', 10, 2 );
}

if ( ! function_exists( 'amy_movie_demo_importer_remap_movie' ) ) {
	function amy_movie_demo_importer_remap_movie( $post, $url ) {
		$post_ids	= Amy_Demo_Importer_Map::instance()->get( 'posts' );

		if ( $post->type == 'amy_movie' ) {
			// banner
			$old_banner 	= unserialize( $post->meta->_movie_banner );
			$old_banner_id 	= $old_banner['movie_banner'];

			if ( isset( $post->meta->_movie_banner ) && isset( $post_ids[ $old_banner_id ] ) ) {
				update_post_meta( $post_ids[ $post->id ][0], '_movie_banner', array( 'movie_banner' => $post_ids[ $old_banner_id ][0] ) );
			}

			// poster
			$old_poster 	= unserialize( $post->meta->_movie_poster );
			$old_poster_id 	= $old_poster['movie_poster'];

			if ( isset( $post->meta->_movie_poster ) && isset( $post_ids[ $old_poster_id ] ) ) {
				update_post_meta( $post_ids[ $post->id ][0], '_movie_poster', array( 'movie_poster' => $post_ids[ $old_poster_id ][0] ) );
			}

			// gallery
			if ( isset( $post->meta->_movie_block_details ) ) {
				$old_details 	= unserialize( $post->meta->_movie_block_details );
				$old_gallery	= $old_details['movie_gallery'];

				$new_gallery	= array();
				$images			= explode( ',', $old_gallery );

				foreach ( $images as $image ) {
					if ( isset( $post_ids[ $image ] ) ) {
						$new_gallery[]	= $post_ids[ $image ][0];
					}
				}

				$new_gallery = implode( ',', $new_gallery );
				$old_details['movie_gallery']	= $new_gallery;

				update_post_meta( $post_ids[ $post->id ][0], '_movie_block_details', $old_details );
			}

			// cinema
			if ( isset( $post->meta->_movie_block_cinema ) ) {
				$content 		= unserialize( $post->meta->_movie_block_cinema );
				$new_content 	= array();

				foreach ( $content['amy_movie_cinema'] as $key => $value ) {
					$value['select_cinema'] 			= $post_ids[ $value['select_cinema'] ][0];
					$new_content['amy_movie_cinema'][] 	= $value;
				}

				update_post_meta( $post_ids[ $post->id ][0], '_movie_block_cinema', $new_content );
			}
		}

		if ( $post->type == 'amy_cinema' ) {
			if ( isset( $post->meta->_cinema_block_options ) ) {
				$details = unserialize( $post->meta->_cinema_block_options );

				if ( ! empty( $details['cinema_banner'] ) ) {
					$details['cinema_banner'] = $post_ids[ $details['cinema_banner'] ][0];
				}

				if ( ! empty( $details['cinema_gallery'] ) ) {
					$new_gallery	= array();
					$images			= explode( ',', $details['cinema_gallery'] );

					foreach ( $images as $image ) {
						if ( isset( $post_ids[ $image ] ) ) {
							$new_gallery[]	= $post_ids[ $image ][0];
						}
					}

					$new_gallery = implode( ',', $new_gallery );

					$details['cinema_gallery'] = $new_gallery;
				}

				update_post_meta( $post_ids[ $post->id ][0], '_cinema_block_options', $details );
			}
		}
	}

	add_action( 'amy_demo_importer_remap_post', 'amy_movie_demo_importer_remap_movie', 10, 3 );
}

if ( ! function_exists( 'amy_movie_demo_importer_remap_movie_term' ) ) {
	function amy_movie_demo_importer_remap_movie_term( $term, $url ) {
		$post_ids	= Amy_Demo_Importer_Map::instance()->get( 'posts' );
		$term_ids	= Amy_Demo_Importer_Map::instance()->get( 'terms' );

		if ( $term->taxonomy == 'amy_director' && isset( $term->meta->_custom_amy_director ) ) {
			$content = unserialize( $term->meta->_custom_amy_director );

			$content['avatar'] = $post_ids[ $content['avatar'] ][0];
			$content['banner'] = $post_ids[ $content['banner'] ][0];

			update_term_meta( $term_ids[ $term->id ][0], '_custom_amy_director', $content );
		}

		if ( $term->taxonomy == 'amy_actor' && isset( $term->meta->_custom_amy_actor ) ) {
			$content = unserialize( $term->meta->_custom_amy_actor );

			$content['avatar'] = $post_ids[ $content['avatar'] ][0];
			$content['banner'] = $post_ids[ $content['banner'] ][0];

			update_term_meta( $term_ids[ $term->id ][0], '_custom_amy_actor', $content );
		}
	}


	add_action( 'amy_demo_importer_remap_term', 'amy_movie_demo_importer_remap_movie_term', 10, 3 );
}

if ( ! function_exists( 'amy_movie_demo_importer_remap_theme_options' ) ) {
	function amy_movie_demo_importer_remap_theme_options( $theme_options, $remote_url ) {
		$map			= Amy_Demo_Importer_Map::instance();
		$post_ids		= $map->get( 'posts' );
		$media_paths	= $map->get( 'media' );

		// map image
		foreach ( array( 'logo', 'logo2x', 'favicon' ) as $key ) {
			if ( isset( $post_ids[ $theme_options[ $key ] ] ) ) {
				$theme_options[ $key ]	= $post_ids[ $theme_options[ $key ] ][0];
			}
		}

		// map background image
		foreach ( array( 'header_background', 'page_header_background', 'footer_block_before_background', 'footer_widgets_before_background' ) as $key ) {
			$media_url		= $theme_options[ $key ]['image'];
			$site_url	= is_multisite() ? network_home_url() : home_url( '/' );

			if ( strpos( $media_url, $remote_url ) === 0 ) {
				if ( strpos( $media_url, 'wp-content/uploads/' ) !== false ) {
					$path	= preg_replace( '`(.*)wp-content/uploads/(.*?)(-\d+x\d+)?\.(jpg|jpeg|png|gif|mp3|mp4|ogv|webm)`', '$2.$4', $media_url );

					if ( isset( $media_paths[ $path ] ) ) {
						$media_url	= $site_url . '/wp-content/uploads/' . $media_paths[ $path ];
					}
				} else {
					if ( strpos( $media_url, 'amy-movie/images' ) !== false ) {
						$media_url	= str_replace( $remote_url, $site_url, $media_url );
					}
				}

				$theme_options[ $key ]['image']	= $media_url;
			}
		}

		// map module image
		foreach ( array( 'top_bar_left', 'top_bar_right', 'footer_copyright_left', 'footer_copyright_right' ) as $key ) {
			$modules	= $theme_options[ $key ];

			if ( $modules && count( $modules ) ) {
				foreach ( $modules as $id => $module ) {
					if ( isset( $module['module_img'] ) && isset( $post_ids[ $module['module_img'] ] ) ) {
						$theme_options[ $key ][ $id ]['module_img']	= $post_ids[ $module['module_img'] ][0];
					}
				}
			}
		}

		return $theme_options;
	}

	add_filter( 'amy_demo_importer_remap_theme_options', 'amy_movie_demo_importer_remap_theme_options', 10, 2 );
}

// remap widgets
if ( ! function_exists( 'amy_movie_demo_importer_remap_widget' ) ) {
	function amy_movie_demo_importer_remap_widget( $options, $widget ) {
		$map		= Amy_Demo_Importer_Map::instance();
		$term_ids	= $map->get( 'terms' );
		$post_ids	= $map->get( 'posts' );
		$widget_ids	= $map->get( 'widgets' );

		if ( $widget->name == 'amy_movie_widget_module' ) {
			if ( isset( $widget->options->logo ) && isset( $post_ids[ $widget->options->logo ] ) ) {
				$options[ $widget_ids[ $widget->name . '-' . $widget->id ] ]['logo']	= $post_ids[ $widget->options->logo ][0];
			}

			if ( isset( $widget->options->banner ) && isset( $post_ids[ $widget->options->banner ] ) ) {
				$options[ $widget_ids[ $widget->name . '-' . $widget->id ] ]['banner']	= $post_ids[ $widget->options->banner ][0];
			}

			if ( isset( $widget->options->gallery ) ) {
				$images		= explode( ',', $widget->options->gallery );
				$gallery	= array();

				foreach ( $images as $image ) {
					if ( isset( $post_ids[ $image ] ) ) {
						$gallery[]	= $post_ids[ $image ][0];
					}
				}

				$options[ $widget_ids[ $widget->name . '-' . $widget->id ] ]['gallery']	= implode( ',', $gallery );
			}
		}

		return $options;
	}

	add_filter( 'amy_demo_importer_remap_widget', 'amy_movie_demo_importer_remap_widget', 10, 2 );
}
