<?php
/*
 Plugin Name:Copuloo
 Plugin URI: https://copuloo.com/
 Description: Easily manage inbound and outbound phone calls and text messages
 Version: 1.0.0
 Author:Copuloo
 Author URI: https://copuloo.com
 Requires at least: 6.2
 Requires PHP: 7.4
 License: GPLv2 or later
 Text Domain: copuloo
 */

 if ( ! defined( 'ABSPATH' ) ) exit;

 // Autoload files
 require_once( plugin_dir_path( __FILE__ ) .'/vendor/twilio-php-main/src/Twilio/autoload.php');

 // Create Pages on activation
 require(plugin_dir_path(__FILE__) . 'lib/config/initiate_pages.php');
 // Create User Role on activation
 require(plugin_dir_path(__FILE__) . 'lib/config/initiate_user_role.php');
 // Create Tables on activation
 require(plugin_dir_path(__FILE__) . 'lib/config/initiate_tables.php');
 // Create encryption Keys on activation
 require(plugin_dir_path(__FILE__) . 'lib/config/initiate_keys.php');
 // Admin Controller
 require(plugin_dir_path(__FILE__) . 'lib/controllers/admin.php');
 // Calls Controller
 require(plugin_dir_path(__FILE__) . 'lib/controllers/messages.php');
 // Dashboard Controller
 require(plugin_dir_path(__FILE__) . 'lib/controllers/dashboard.php');
 // Token Controller
 require(plugin_dir_path(__FILE__) . 'lib/controllers/webcalling.php');

 class CopulooClass
 {
   public function __construct()
   {
     // Change styling for Copuloo plugin dashboard
     add_action('admin_enqueue_scripts', array($this,'copuloo_load_custom_blank_page_styles'), 9999);

     // Only apply the Bootstrap "light" theme on the Copuloo dashboard page
     add_action('admin_enqueue_scripts', function ($hook) {
     if ($hook === 'toplevel_page_copuloo_dashboard') {
        add_action('admin_head', function () {
            // Set the Bootstrap theme to light
            echo '<script>document.documentElement.setAttribute("data-bs-theme", "light");</script>';
        });
      }
     });

     // Add guest video chat page
     add_action('template_redirect', function () {
      $page_id = get_queried_object_id();
      if ($page_id == get_option('copuloo_user_guest_video_page') && !empty(get_option('copuloo_user_guest_video_page'))) {
          include plugin_dir_path(__FILE__) . 'lib/views/dashboard/guestteams.php';
          exit;
      }
     });

     // Handling incoming calls and texts
     add_action( 'admin_post_nopriv_copuloo_calls_logic', array(new CopulooWebCallingController, 'calls_logic') ); // Main incoming calls logic
     add_action( 'admin_post_nopriv_copuloo_incoming_texts', array(new CopulooManageMessagesController, 'incoming_texts') );
     // Get token for browser enabled calling
     add_action( 'admin_post_copuloo_browser_token', array(new CopulooWebCallingController, 'browser_token') );
     add_action( 'admin_post_copuloo_browser_video_token', array(new CopulooWebCallingController, 'browser_video_token') );

     // Add menu inside wordpress admin panel
     add_action( 'admin_menu', array($this, 'copuloo_admin_page') );
     // Add custom dashboard post requests
     add_action( 'admin_post_copuloo_update_router_table', array(new CopulooDashboardController, 'update_router_table') ); // update basic information in copuloo_phone_details table
     add_action( 'admin_post_copuloo_update_ai_assistant', array(new CopulooDashboardController, 'update_ai_assistant') );
     add_action( 'admin_post_copuloo_search_numbers', array(new CopulooDashboardController, 'search_numbers') );
     add_action( 'admin_post_copuloo_request_number', array(new CopulooDashboardController, 'request_number') );
     add_action( 'admin_post_copuloo_re_request', array(new CopulooDashboardController, 're_request') );
     add_action( 'admin_post_copuloo_new_contact', array(new CopulooDashboardController, 'new_contact') );
     add_action( 'admin_post_copuloo_update_contact', array(new CopulooDashboardController, 'update_contact') );
     add_action( 'admin_post_copuloo_remove_contact', array(new CopulooDashboardController, 'remove_contact') );
     add_action( 'admin_post_copuloo_remove_messages', array(new CopulooDashboardController, 'remove_messages') );
     add_action( 'admin_post_copuloo_release_managed_number_user', array(new CopulooDashboardController, 'release_managed_number') ); // Gives users the ability to remove their managed number
     add_action( 'admin_post_copuloo_assistant_setup', array(new CopulooDashboardController, 'assistant_setup') );
     add_action( 'admin_post_copuloo_assistant_answer_calls', array(new CopulooDashboardController, 'assistant_answer_calls') );
     add_action( 'admin_post_copuloo_assistant_answer_texts', array(new CopulooDashboardController, 'assistant_answer_texts') );
     add_action( 'admin_post_copuloo_voice_setup', array(new CopulooDashboardController, 'voice_setup') );
     add_action( 'admin_post_copuloo_send_text', array(new CopulooDashboardController, 'send_text') );
     add_action( 'wp_ajax_copuloo_get_video_call_token', array(new CopulooDashboardController, 'get_video_call_token') );
     //if (!empty(get_option('copuloo_user_guest_video_page')) || get_option('copuloo_user_guest_video_page') != 'copuloo_disabled_copuloo') { add_action( 'wp_ajax_nopriv_copuloo_get_video_call_token', array(new CopulooDashboardController, 'get_video_call_token') ); }

     // Add administrator post requests
     add_action( 'admin_post_copuloo_add_managed_number', array(new CopulooAdminController, 'add_managed_number') );
     add_action( 'admin_post_copuloo_remove_managed_number', array(new CopulooAdminController, 'remove_managed_number') );
     add_action( 'admin_post_copuloo_get_user_id', array(new CopulooAdminController, 'get_user_id') );
     add_action( 'admin_post_copuloo_setup_api_keys', array(new CopulooAdminController, 'setup_api_keys') );
     add_action( 'admin_post_copuloo_additional_setup', array(new CopulooAdminController, 'additional_setup') );
     add_action( 'admin_post_copuloo_update_license_key', array(new CopulooAdminController, 'update_license_key') );
     add_action( 'admin_post_copuloo_auto_setup_api_keys', array(new CopulooAdminController, 'auto_setup_api_keys') );

     // Add the database tables
     register_activation_hook( __FILE__, array(new CopulooInitiateTablesClass, 'initiate_tables') );
     // Add new role for users that only have access to Copuloo dashboard pages
     register_activation_hook( __FILE__, array(new CopulooInitiateUserRoleClass, 'register_user_role') );
   }

   function copuloo_load_custom_blank_page_styles($hook)
   {
     // This only applies to Copuloo dashboard for a clean interface
     if ($hook == 'toplevel_page_copuloo_dashboard')
     {
       // Dequeue Latepoint styles in the Copuloo dashboard when the plugin is active
       wp_deregister_style( 'latepoint-admin' );
       wp_dequeue_style( 'latepoint-admin' );
       wp_deregister_style( 'latepoint-blocks' );
       wp_dequeue_style( 'latepoint-blocks' );
       wp_dequeue_style( 'wp-components' );
       
       // Dequeue & Deregister
       wp_deregister_style( 'dashicons' );
       wp_dequeue_style( 'dashicons' );
       
       // Remove WordPress admin bar
       remove_all_actions('in_admin_header');
       remove_all_actions('in_admin_footer');
       // Deregister notifications
       remove_all_actions('admin_notices');
       remove_all_actions('all_admin_notices');
       remove_all_actions('user_admin_notices');
       remove_all_actions('network_admin_notices');
       // Enque style
       wp_enqueue_style( 'bootstrap', plugins_url( 'public/css/copuloo.bootstrap.css', __FILE__ ), array(), '1.0', 'all' );
       wp_enqueue_style( 'fontawesome', plugins_url( 'public/fontawesome/css/fontawesome.css', __FILE__ ), array(), '1.0', 'all' );
       wp_enqueue_style( 'solid', plugins_url( 'public/fontawesome/css/solid.css', __FILE__ ), array(), '1.0', 'all' );
       wp_enqueue_style( 'copuloo-css', plugins_url( 'public/css/copuloo.css', __FILE__ ), array(), '1.1', 'all' );
       // Enque scripts
       wp_enqueue_script( 'copuloo-main', plugins_url( 'public/js/copuloo.bootstrap.min.js', __FILE__ ), array('jquery'), '1.0', true );
       wp_enqueue_script( 'copuloo-twilio-min', plugins_url( 'public/js/twilio.min.js', __FILE__ ), array('jquery'), '1.0', true );
       wp_enqueue_script( 'copuloo-js', plugins_url( 'public/js/copuloo.js', __FILE__ ), array('jquery'), '1.1', true );
       wp_localize_script('copuloo-js', 'CopulooBrowserCallingVars', array(
          'tokenUrl' => esc_url(admin_url('admin-post.php')),
          'userID'   => get_current_user_id(),
          'user_nonce' => wp_create_nonce('Copuloo'),
       ));
     }
   }

   // Add admin page to Wordpress admin navigation bar
   function copuloo_admin_page()
   {
     global $wp_roles;
     $wp_roles->add_cap( 'administrator', 'copuloo_view_dashboard' );
     $wp_roles->add_cap( 'copuloo', 'copuloo_view_dashboard' );

     add_menu_page(
       __( 'Copuloo Call Center', 'copuloo' ),
       'Copuloo',
       'copuloo_view_dashboard',
       'copuloo_dashboard',
       function ()
       {
         $copulooDashboardUrl = admin_url('admin.php?page=copuloo_dashboard');
         include(plugin_dir_path(__FILE__) . 'lib/components/alerts.php' );
         include(plugin_dir_path(__FILE__) . 'lib/components/countrycode.php' );
         include(plugin_dir_path(__FILE__) . 'lib/components/countries.php' );
         include(plugin_dir_path(__FILE__) . 'lib/components/timezone.php' );
         include(plugin_dir_path(__FILE__) . 'lib/components/formatnumber.php' );
         include(plugin_dir_path(__FILE__) . 'lib/components/timeelapsed.php' );
         include(plugin_dir_path(__FILE__) . 'app.php' );
       },
       'dashicons-phone',
       99
     );
     if ( current_user_can('administrator') )
     {
       add_submenu_page(
       	'copuloo_dashboard',
       	'Copuloo - Setup',
       	'Settings',
       	'manage_options',
       	'copuloo_setup',
       	function () { $current_page = 'setup'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/components/timeelapsed.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/setup.php' ); },
         null
       );
       add_submenu_page(
       	'copuloo_dashboard',
       	'Copuloo - Info',
       	'Information',
       	'manage_options',
       	'copuloo_info',
       	function () { $current_page = 'info'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/info.php' ); },
         null
       );
       add_submenu_page(
       	'copuloo_dashboard',
       	'Copuloo - Active',
       	'Active Numbers',
       	'manage_options',
       	'copuloo_activenumbers',
       	function () { $current_page = 'activenumbers'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/components/formatnumber.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/active_numbers.php' ); },
         null
       );
       add_submenu_page(
       	'copuloo_dashboard',
       	'Copuloo - Released',
       	'Released Numbers',
       	'manage_options',
       	'copuloo_released',
       	function () { $current_page = 'released'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/components/formatnumber.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/released.php' ); },
         null
       );
       add_submenu_page(
       	'copuloo_dashboard',
       	'Copuloo - Add',
       	'Add Number',
       	'manage_options',
       	'copuloo_add',
       	function () { $current_page = 'add'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/components/countrycode.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/add.php' ); },
         null
       );
       add_submenu_page(
       	'copuloo_dashboard',
       	'Copuloo - Remove',
       	'Remove Number',
       	'manage_options',
       	'copuloo_remove',
       	function () { $current_page = 'remove'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/components/countrycode.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/remove.php' ); },
         null
       );
       add_submenu_page(
       	'copuloo_dashboard',
       	'Copuloo - License',
       	'License',
       	'manage_options',
       	'copuloo_license',
       	function () { $current_page = 'license'; include(plugin_dir_path(__FILE__) . 'lib/views/admin/header.php' ); include(plugin_dir_path(__FILE__) . 'lib/views/admin/license.php' ); },
         null
       );
     }
   }

}
new CopulooClass;
?>
