<?php
/**
 * Code Manager dashboard class
 *
 * @package Code_Manager
 */

namespace Code_Manager {

	/**
	 * Manages the plugin dashboard in the back-end
	 */
	class Code_Manager_Dashboard {

		/**
		 * Add plugin dashboard to plugin back-end page
		 *
		 * @return void
		 */
		public static function add_dashboard() {
			$dashboard = new Code_Manager_Dashboard();
			$dashboard->dashboard();
		}

		/**
		 * Check if menu is enabled
		 *
		 * @return bool
		 */
		public static function menu_enabled() {
			return true;
		}

		/**
		 * Add dashboard to page top
		 *
		 * @return void
		 */
		public function dashboard() {
			if (
				isset( $_REQUEST['page'] ) &&
				(
					\Code_Manager_Admin::PAGE_TAB === $_REQUEST['page'] ||
					\Code_Manager_Admin::PAGE_MAIN === $_REQUEST['page'] ||
					\Code_Manager_Admin::PAGE_SETTINGS === $_REQUEST['page']
				)
			) {
				$this->dashboard_default();
				$this->dashboard_mobile();

				if ( \Code_Manager_Admin::PAGE_TAB === $_REQUEST['page'] ) {
					$this->toolbar_tabmode();
				} else {
					if (
						\Code_Manager_Admin::PAGE_MAIN === $_REQUEST['page'] &&
						(
							! isset( $_REQUEST['action'] ) ||
							(
								'-1' === $_REQUEST['action']
							)
						)
					) {
						$this->toolbar_listmode();
					}
				}
			}
		}

		/**
		 * Default dashboard, using full page width
		 *
		 * @return void
		 */
		protected function dashboard_default() {
			?>
			<div id="cm-dashboard" style="display:none">
				<div class="cm-dashboard">
					<div class="cm-dashboard-group cm-dashboard-group-code">
						<div class="icons">
							<a class="cm-dashboard-item cm_tooltip_icons" href="<?php echo admin_url( 'admin.php' ); ?>?page=<?php echo \Code_Manager_Admin::PAGE_MAIN; ?>" title="Standard WordPress list view">
								<i class="fas fa-table-list"></i>
								<div class="label">List mode</div>
							</a>
							<a class="cm-dashboard-item cm_tooltip_icons" href="<?php echo admin_url( 'admin.php' ); ?>?page=<?php echo \Code_Manager_Admin::PAGE_TAB; ?>" title="Open multiple code editors simultaneously">
								<i class="fas fa-folder"></i>
								<div class="label">Tab mode</div>
							</a>
							<a class="cm-dashboard-item cm_tooltip_icons" href="https://code-manager.com/code/" title="Download reusable code from plugin website" target="_blank">
								<i class="fas fa-cloud-download"></i>
								<div class="label">Download</div>
							</a>
						</div>
						<div class="subject">Code</div>
					</div>
					<div class="cm-dashboard-group cm-dashboard-group-settings">
						<div class="icons">
							<a class="cm-dashboard-item cm_tooltip_icons" href="<?php echo admin_url( 'options-general.php?page=' . \Code_Manager_Admin::PAGE_SETTINGS ); ?>" title="Plugin Settings">
								<i class="fas fa-cog"></i>
								<div class="label">Settings</div>
							</a>
							<?php
							if ( code_manager_fs()->is_registered() ) {
								?>
								<a class="cm-dashboard-item cm_tooltip_icons" href="<?php echo admin_url( 'admin.php' ); ?>?page=code_manager-account" title="Manage Account">
									<i class="fas fa-user"></i>
									<div class="label">Account</div>
								</a>
								<?php
							}
							?>
							<a class="cm-dashboard-item cm_tooltip_icons" target="_blank" href="https://code-manager.com/pricing/" title="Online Pricing, Licensing and Ordering">
								<i class="fas fa-hand-holding-usd"></i>
								<div class="label">Pricing</div>
							</a>
							<?php
							$menufound = false;
							if ( self::menu_enabled() ) {
								global $submenu;
								$plugin_navigation_default_page = get_option( 'plugin_navigation_default_page' );
								if ( isset( $submenu[ $plugin_navigation_default_page ] ) ) {
									foreach ( $submenu[ $plugin_navigation_default_page ] as $pluginmenu ) {
										if ( 'code_manager-pricing' === $pluginmenu[2] ) {
											$menufound = true;
											break;
										}
									}
								}
							} else {
								$menufound = true;
							}
							if ( $menufound ) {
								?>
								<a class="cm-dashboard-item cm_tooltip_icons" href="<?php echo admin_url( 'admin.php' ); ?>?page=code_manager-pricing" title="Upgrade plugin from dashboard">
									<i class="fas fa-gem"></i>
									<div class="label">Upgrade</div>
								</a>
								<?php
							}
							?>
						</div>
						<div class="subject">Manage</div>
					</div>
					<div class="cm-dashboard-group cm-dashboard-group-support">
						<div class="icons">
							<a class="cm-dashboard-item cm_tooltip_icons" target="_blank" href="https://code-manager.com/blog/docs/index/getting-started/read-this-first/" title="Online Help and Documentation">
								<i class="fas fa-question-circle"></i>
								<div class="label">Docs</div>
							</a>
							<a class="cm-dashboard-item cm_tooltip_icons" target="_blank" href="https://wordpress.org/support/plugin/code-manager/" title="Public Support Forum">
								<i class="fas fa-life-ring"></i>
								<div class="label">Forum</div>
							</a>
							<?php
							if ( code_manager_fs()->is_premium() ) {
								?>
								<a class="cm-dashboard-item cm_tooltip_icons" target="_blank" href="https://users.freemius.com/store/2612" title="Premium Support">
									<i class="fas fa-ambulance"></i>
									<div class="label">Premium</div>
								</a>
								<?php
							}
							?>
						</div>
						<div class="subject">Support</div>
					</div>
				</div>
			</div>
			<?php
		}

		/**
		 * Mobile dashboard, using hamburger menu icon
		 *
		 * @return void
		 */
		protected function dashboard_mobile() {
			?>
			<div id="cm-dashboard-mobile" style="display:none">
				<div id="cm-dashboard-drop-down">
					<div class="cm_nav_toggle" onclick="toggleMenu()"><i class="fas fa-bars"></i></div>
					<div class="cm_nav_title">Code Manager</div>
				</div>
				<ul>
					<li class="menu-item"><a href="<?php echo admin_url( 'admin.php' ); ?>?page=code_manager"><i class="fas fa-table-list"></i> List mode</a></li>
					<li class="menu-item"><a href="<?php echo admin_url( 'admin.php' ); ?>?page=<?php echo \Code_Manager_Admin::PAGE_TAB; ?>"><i class="fas fa-folder"></i> Tab mode</a></li>
					<li class="menu-item cm-separator"><a href="https://code-manager.com/code/" target="_blank"><i class="fas fa-cloud-download"></i> Download reusable code</a></li>
					<li class="menu-item"><a href="<?php echo admin_url( 'options-general.php?page=code_manager_settings' ); ?>"><i class="fas fa-cog"></i> Settings</a></li>
					<li class="menu-item"><a href="<?php echo admin_url( 'admin.php' ); ?>?page=code_manager-account"><i class="fas fa-user"></i> Account</a></li>
					<?php
					$menufound = false;
					if ( self::menu_enabled() ) {
						global $submenu;
						$plugin_navigation_default_page = get_option( 'plugin_navigation_default_page' );
						if ( isset( $submenu[ $plugin_navigation_default_page ] ) ) {
							foreach ( $submenu[ $plugin_navigation_default_page ] as $pluginmenu ) {
								if ( 'code_manager-pricing' === $pluginmenu[2] ) {
									$menufound = true;
									break;
								}
							}
						}
					} else {
						$menufound = true;
					}
					?>
					<li class="menu-item <?php echo $menufound ? '' : 'cm-separator'; ?>"><a href="https://code-manager.com/pricing/" target="_blank"><i class="fas fa-hand-holding-usd"></i> Pricing</a></li>
					<?php
					if ( $menufound ) {
						?>
						<li class="menu-item cm-separator"><a href="<?php echo admin_url( 'admin.php' ); ?>?page=code_manager-pricing"><i class="fas fa-gem"></i> Upgrade</a></li>
						<?php
					}
					?>
					<li class="menu-item"><a target="_blank" href="https://code-manager.com/blog/docs/index/"><i class="fas fa-circle-question"></i> Online Documentation</a></li>
					<li class="menu-item"><a target="_blank" href="https://wordpress.org/plugins/code-manager/"><i class="fas fa-life-ring"></i> Support Forum</a></li>
					<?php
					if ( code_manager_fs()->is_premium() ) {
						?>
						<li class="menu-item"><a target="_blank" href="<?php echo admin_url( 'admin.php' ); ?>?page=code_manager-wp-support-forum"><i class="fas fa-ambulance"></i> Premium Support</a></li>
						<?php
					}
					?>
				</ul>
			</div>
			<?php
		}

		/**
		 * Add tab mode toolbar to dashboard
		 *
		 * @return void
		 */
		protected function toolbar_tabmode() {
			?>
			<div class="cm-dashboard-toolbar">
				<div>
					<div>
						<i id="code_manager_new" class="fas fa-plus-circle cm_tooltip" title="Add new code"></i>
						<br/>
						Add new code
					</div>
				</div>
				<div>
					<div>
						<i id="code_manager_open" class="fas fa-folder-open cm_tooltip" title="Open existing code"></i>
						<br/>
						Open existing code
					</div>
				</div>
			</div>
			<?php
		}

		/**
		 * Add list mode toolbar to dashboard
		 *
		 * @return void
		 */
		protected function toolbar_listmode() {
			?>
			<div class="cm-dashboard-toolbar">
				<div>
					<div>
						<i id="code_manager_new" class="fas fa-plus-circle cm_tooltip" title="Add new code" onclick="window.location.href='?page=<?php echo \Code_Manager_Admin::PAGE_MAIN; ?>&action=new'"></i>
						<br/>
						Add new code
					</div>
				</div>
				<div>
					<div>
						<i id="code_manager_import" class="fas fa-cloud-upload cm_tooltip" title="Import code" onclick="jQuery('#upload_file_container').toggle()"></i>
						<br/>
						Import code
					</div>
				</div>
			</div>
			<?php
		}

	}

}
