<?php 
// version 1.0
require_once(dirname(__FILE__)."/functions.php");
require_once(dirname(__FILE__)."/actions.php");
require_once(dirname(__FILE__)."/do-action.php");
require_once(dirname(__FILE__)."/filters.php");
require_once(dirname(__FILE__)."/table-functions.php");

class messaging {

	public static $plugin_name = "messaging";
	public static $plugin_root = 'contents\plugins\messaging';
	public static $plugin_ref = "Messaging";

	public function __construct () {
		
	}

	public static function install() {
		$common_access = ['messaging_menu', 'support_ticket', "view_ticket"];
		add_common_access($common_access);
		$super_admin = ['messaging_menu', 'support', 'send_bulk_sms', 'sms_delivery_report', 'sms_payment_report', 'sms_list', 'edit_sms_list', 'support_ticket', "open_ticket_for_others", "view_ticket", "respond_to_others_support_tickets", "send_bulk_email", "create_announcement", "edit_announcement"]; //content owner
		$super_admin_tech = ['messaging_menu', 'support', 'send_bulk_sms', 'sms_delivery_report', 'sms_payment_report', 'sms_list', 'edit_sms_list', 'support_ticket', "open_ticket_for_others", "view_ticket", "respond_to_others_support_tickets", "delete_support_ticket", "send_bulk_email", "create_announcement", "edit_announcement"]; //technical owner
		$admin = ['messaging_menu', 'support', 'send_bulk_sms', 'sms_list', 'edit_sms_list', 'support_ticket', "open_ticket_for_others", "view_ticket", "respond_to_others_support_tickets", "send_bulk_email", "create_announcement", "edit_announcement"]; //content owner
		$admin_tech = ['messaging_menu', 'support', 'support_ticket', "open_ticket_for_others", "view_ticket", "respond_to_others_support_tickets", "create_announcement", "edit_announcement"]; //technical owner
		$member = ['messaging_menu', 'support', 'support_ticket', "view_ticket"];
		$pending = ['messaging_menu', 'support_ticket', "view_ticket"];
		$unverified = ['messaging_menu', 'support_ticket', "view_ticket"];
		$se = [];
		
		update_role('software_engineer', array_unique(array_merge($super_admin, $admin, $super_admin_tech, $admin_tech, $pending, $unverified, $member, $se)));
		update_role('super_admin', $super_admin);
		update_role('admin', $admin);
		update_role('super_admin_tech', $super_admin_tech);
		update_role('admin_tech', $admin_tech);
		update_role('member', $member);
		update_role('pending', $pending);
		update_role('unverified', $unverified);


		$post = new post;
		register_admin_page(array(
			'title' => 'Chat',
			'callback' => 'support_cb',
			'access' => 'support',
			'permalink' => 'support'
		));

		register_admin_page(array(
			'title' => 'Send Bulk SMS',
			'callback' => 'send_bulk_sms_cb',
			'access' => 'send_bulk_sms',
			'permalink' => 'send-bulk-sms'
		));

		register_admin_page(array(
			'title' => 'SMS Delivery Report',
			'callback' => 'sms_delivery_report_cb',
			'access' => 'sms_delivery_report',
			'permalink' => 'sms-delivery-report'
		));

		register_admin_page(array(
			'title' => 'SMS Payment Report',
			'callback' => 'sms_payment_report_cb',
			'access' => 'sms_payment_report',
			'permalink' => 'sms-payment-report'
		));

		register_admin_page(array(
			'title' => 'SMS List',
			'callback' => 'sms_list_cb',
			'access' => 'sms_list',
			'permalink' => 'sms-list'
		));

		register_admin_page(array(
			'title' => 'Edit SMS List',
			'callback' => 'edit_sms_list_cb',
			'access' => 'edit_sms_list',
			'permalink' => 'edit-sms-list'
		));

		register_admin_page(array(
			'title' => 'Support Ticket',
			'callback' => 'support_ticket_cb',
			'access' => 'support_ticket',
			'permalink' => 'support-ticket'
		));

		register_admin_page(array(
			'title' => 'View Ticket',
			'callback' => 'view_ticket_cb',
			'access' => 'view_ticket',
			'permalink' => 'view-ticket'
		));

		register_admin_page(array(
			'title' => 'Send Bulk Email',
			'callback' => 'send_bulk_email_cb',
			'access' => 'send_bulk_email',
			'permalink' => 'send-bulk-email'
		));

		register_admin_page(array(
			'title' => 'Announcement',
			'callback' => 'create_announcement_cb',
			'access' => 'create_announcement',
			'permalink' => 'create-announcement'
		));

		register_admin_page(array(
			'title' => 'Edit Announcement',
			'callback' => 'edit_announcement_cb',
			'access' => 'edit_announcement',
			'permalink' => 'edit-announcement'
		));

		register_admin_menu(array(
			'id' => 'messaging',
			'title' => 'Messaging',
			'icon' => 'far fa-envelope',
			'page' => '',
			'access' => 'messaging_menu',
			'order' => 5
		));

		register_admin_submenu(array(
			'mm_id' => 'messaging',
			'id' => 'support',
			'title' => 'Chat',
			'icon' => '',
			'page' => 'support',
			'access' => 'support'
		));

		register_admin_submenu(array(
			'mm_id' => 'messaging',
			'id' => 'send_bulk_sms',
			'title' => 'Send Bulk SMS',
			'icon' => '',
			'page' => 'send-bulk-sms',
			'access' => 'send_bulk_sms'
		));

		register_admin_submenu(array(
			'mm_id' => 'messaging',
			'id' => 'send_bulk_email',
			'title' => 'Send Bulk Email',
			'icon' => '',
			'page' => 'send-bulk-email',
			'access' => 'send_bulk_email'
		));

		register_admin_submenu(array(
			'mm_id' => 'messaging',
			'id' => 'sms_delivery_report',
			'title' => 'Delivery Report',
			'icon' => '',
			'page' => 'sms-delivery-report',
			'access' => 'sms_delivery_report'
		));

		register_admin_submenu(array(
			'mm_id' => 'messaging',
			'id' => 'sms_list',
			'title' => 'SMS List',
			'icon' => '',
			'page' => 'sms-list',
			'access' => 'sms_list'
		));

		register_admin_submenu(array(
			'mm_id' => 'messaging',
			'id' => 'support_ticket',
			'title' => 'Support Ticket',
			'icon' => '',
			'page' => 'support-ticket',
			'access' => 'support_ticket'
		));

		register_admin_submenu(array(
			'mm_id' => 'messaging',
			'id' => 'sms_payment_report',
			'title' => 'Payment Report',
			'icon' => '',
			'page' => 'sms-payment-report',
			'access' => 'sms_payment_report'
		));

		register_admin_submenu(array(
			'mm_id' => 'messaging',
			'id' => 'create_announcement',
			'title' => 'Announcement',
			'icon' => '',
			'page' => 'create-announcement',
			'access' => 'create_announcement'
		));

		add_action('header_prompts', 'display_new_messages');
		add_action('additional_profile_item', 'add_mail_to_dash');
		add_action('additional_profile_item', 'bulk_sms_bal_to_dash');
		add_action('more_settings_field_left', 'messaging_settings_fields');
		add_action('save_more_settings_field', 'save_messaging_settings');
		add_action("after_new_comment_support_ticket", "support_ticket_after_new_comment");
		add_action("add_sms_list_to_form", "add_sms_list_to_form");
		add_action("msg_announcement", "msg_announcement");
		add_action("before_render_page", "msg_scrolling_announcement");
		remove_action("before_render_content", "msg_scrolling_announcement");

		apply_filters("dynamic_select_value_phone_number", "dynamic_select_value_phone_number");
		self::setup_default_list();
	}

	public static function uninstall() {
		unregister_admin_page([
			'support', 'send-bulk-sms', 'sms-delivery-report', 'sms-payment-report', 'sms-list', 'edit-sms-list'
		]);

		unregister_admin_menu([
			'messaging'
		]);

		unregister_admin_submenu([
			'sms_list', 'edit_sms_list'
		]);

		remove_action('header_prompts', 'display_new_messages');
		remove_action('additional_profile_item', 'add_mail_to_dash');
		remove_action('additional_profile_item', 'bulk_sms_bal_to_dash');
		remove_action('more_settings_field_left', 'messaging_settings_fields');
		remove_action('save_more_settings_field', 'save_messaging_settings');
		remove_action("after_new_comment_support_ticket", "support_ticket_after_new_comment");
		remove_action("add_sms_list_to_form", "add_sms_list_to_form");
	}

	public static function setup_default_list() {
		$msg = MessagingOps::getInstance();
		// Add Roles
		$roles = get_system_roles();
		if(_count($roles)) {
			foreach ($roles as $role_name => $role_slug) {
				$msg->add_to_list("All ".$role_name."(s)", "get_users_by_role", $role_slug);
			}
		}
	}
}