<?php

namespace FSPoster\App\Providers;

use FSPoster\App\Controller\Ajax;

trait PluginMenu
{
	private $menus = ['account' , 'settings' , 'app' , 'posts' , 'insights' , 'schedule' , 'schedule' , 'share'];

	public function initMenu()
	{
		add_action('init' , function()
		{
			$this->getNotifications();
			$res1 = $this->checkLicense();
			if( false === $res1 )
			{
				return;
			}

			$plgnVer = Helper::getOption('poster_plugin_installed' , '0', true);

			$hideFSPosterForRoles = explode('|' , Helper::getOption('hide_menu_for' , ''));

			$userInf = wp_get_current_user();
			$userRoles = (array)$userInf->roles;

			if( !in_array('administrator' , $userRoles) )
			{
				foreach ($userRoles AS $roleId)
				{
					if( in_array( $roleId , $hideFSPosterForRoles ) )
					{
						return;
					}
				}
			}

			if( empty( $plgnVer ) )
			{
				add_action( 'admin_menu', function()
				{
					add_menu_page(
						'FS Poster',
						'FS Poster',
						'read',
						'fs-poster',
						array( $this, 'app_install' ),
						Helper::assets('images/logo_xs.png?new'),
						90
					);
				});
				return;
			}
			else if( $plgnVer != Helper::getVersion() )
			{
				$fsPurchaseKey = Helper::getOption('poster_plugin_purchase_key' , '', true);
				if( $fsPurchaseKey != '' )
				{
					$result = Ajax::updatePlugin( $fsPurchaseKey );
					if( $result[0] == false )
					{
						add_action( 'admin_menu', function()
						{
							add_menu_page(
								'FS Poster',
								'FS Poster',
								'read',
								'fs-poster',
								array( $this, 'app_update' ),
								Helper::assets('images/logo_xs.png?new'),
								90
							);
						});

						return;
					}
				}
				else
				{
					add_action( 'admin_menu', function()
					{
						add_menu_page(
							'FS Poster',
							'FS Poster',
							'read',
							'fs-poster',
							array( $this, 'app_update' ),
							Helper::assets('images/logo_xs.png?new'),
							90
						);
					});

					return;
				}

			}

			add_action( 'admin_menu', function()
			{
				add_menu_page(
					'FS Poster',
					'FS Poster',
					'read',
					'fs-poster',
					array( $this, 'app_base' ),
					Helper::assets('images/logo_xs.png?new'),
					90
				);

				add_submenu_page( 'fs-poster', esc_html__('Accounts' , 'fs-poster'), esc_html__('Accounts' , 'fs-poster'),
					'read', 'fs-poster' , array( $this, 'app_base' ));

				add_submenu_page( 'fs-poster', esc_html__('Schedules' , 'fs-poster'), esc_html__('Schedule' , 'fs-poster'),
					'read', 'fs-poster-schedule' , array( $this, 'app_base' ));

				add_submenu_page( 'fs-poster', esc_html__('Share' , 'fs-poster'), esc_html__('Share' , 'fs-poster'),
					'read', 'fs-poster-share' , array( $this, 'app_base' ));

				add_submenu_page( 'fs-poster', esc_html__('Logs' , 'fs-poster'), esc_html__('Logs' , 'fs-poster'),
					'read', 'fs-poster-posts' , array( $this, 'app_base' ));

				add_submenu_page( 'fs-poster', esc_html__('Insights' , 'fs-poster'), esc_html__('Insights' , 'fs-poster'),
					'read', 'fs-poster-insights' , array( $this, 'app_base' ));

				add_submenu_page( 'fs-poster', esc_html__('Apps' , 'fs-poster'), esc_html__('Apps' , 'fs-poster'),
					'read', 'fs-poster-app' , array( $this, 'app_base' ));

				if( current_user_can('administrator') )
				{
					add_submenu_page( 'fs-poster', esc_html__('Settings' , 'fs-poster'), esc_html__('Settings' , 'fs-poster'),
						'read', 'fs-poster-settings' , array( $this, 'app_base' ));
				}

			} );
		});
	}

	public function app_base()
	{
		$menuKey = Request::get('page' , reset($this->menus) , 'string');
		$menuKey = str_replace('fs-poster-' , '' , $menuKey);

		if( !in_array($menuKey , $this->menus) )
		{
			$menuKey = reset($this->menus);
		}

		Helper::view('app_base', [
			'menuKey'	=>	$menuKey
		]);
	}

	public function app_install()
	{
		Helper::view('app_install');
	}

	public function app_disable()
	{
		register_uninstall_hook( FS_ROOT_DIR . '/init.php', [ Helper::class, 'removePlugin' ]);

		Helper::view('app_disable');
	}

	public function app_update()
	{
		Helper::view('app_update');
	}

	public function getNotifications()
	{
		$lastTime = Helper::getOption('license_last_checked_time', 0);

		if( Date::epoch() - $lastTime < 10 * 60 * 60 )
			return;

		$fsPurchaseKey = Helper::getOption('poster_plugin_purchase_key' , '', true);

		$checkPurchaseCodeURL = FS_API_URL . "api.php?act=get_notifications&purchase_code=" . $fsPurchaseKey . "&domain=" . network_site_url();
		$result2 = Curl::getURL($checkPurchaseCodeURL);
		$result = json_decode($result2 , true);

		if( $result['action'] == 'empty' )
		{
			Helper::setOption('plugin_alert', '', true);
			Helper::setOption('plugin_disabled', '0', true);
		}
		else if( $result['action'] == 'warning' && !empty( $result['message'] ) )
		{
			Helper::setOption('plugin_alert', $result['message'], true);
			Helper::setOption('plugin_disabled', '0', true);
		}
		else if( $result['action'] == 'disable' )
		{
			if( !empty( $result['message'] ) )
			{
				Helper::setOption('plugin_alert', $result['message'], true);
			}
			Helper::setOption('plugin_disabled', '1', true);
		}
		else if( $result['action'] == 'error' )
		{
			if( !empty( $result['message'] ) )
			{
				Helper::setOption('plugin_alert', $result['message'], true);
			}
			Helper::setOption('plugin_disabled', '2', true);
		}

		if( !empty( $result['remove_license'] ) )
		{
			Helper::deleteOption('poster_plugin_installed', true);
			Helper::deleteOption('poster_plugin_purchase_key', true);
		}

		Helper::setOption('license_last_checked_time', time());
	}

	public function checkLicense()
	{
		$alert = Helper::getOption('plugin_alert', '', true);
		$disabled = Helper::getOption('plugin_disabled', '0', true);

		if( $disabled == '1' )
		{
			add_action( 'admin_menu', function()
			{
				add_menu_page(
					'FS Poster (!)',
					'FS Poster (!)',
					'read',
					'fs-poster',
					array( $this, 'app_disable' ),
					Helper::assets('images/logo_xs.png?new'),
					90
				);
			});

			return false;
		}
		else if( $disabled == '2' )
		{
			if( !empty( $alert ) )
				print $alert;

			exit();
		}

		if( !empty( $alert ) )
		{
			add_action( 'admin_notices', function() use( $alert )
			{
				?>
				<div class="notice notice-error">
					<p><?php _e( $alert, 'fs-poster' ); ?></p>
				</div>
				<?php
			});
		}

		return true;
	}

}
