<?php
defined('ABSPATH') or die('No script kiddies please!!');
if (!class_exists('CIWS_Admin')) {
    class CIWS_Admin extends CIWS_Library {
        function __construct() {
            add_action('admin_menu', array($this, 'ciws_admin_menu'));
        }
        function ciws_admin_menu() {
            add_menu_page(
                esc_html__('Chat in Website', 'chat-in-website'),
                esc_html__('Chat in Website', 'chat-in-website'),
                'manage_options',
                'chat-in-website',
                array($this, 'ciws_generate_main_page'),
                'dashicons-whatsapp'
            );



            add_submenu_page(
                'chat-in-website',
                esc_html__('Add New Account', 'chat-in-website'),
                esc_html__('Add New Account', 'chat-in-website'),
                'manage_options',
                'ciws-add-new-account',
                array($this, 'ciws_add_new_account_settings_page')
            );

            add_submenu_page(
                'chat-in-website',
                esc_html__('Floating Icon', 'chat-in-website'),
                esc_html__('Floating Icon', 'chat-in-website'),
                'manage_options',
                'ciws-floating-icon',
                array($this, 'ciws_floating_icon_settings_page')
            );

            add_submenu_page(
                'chat-in-website',
                esc_html__('Settings', 'chat-in-website'),
                esc_html__('Settings', 'chat-in-website'),
                'manage_options',
                'ciws-settings',
                array($this, 'ciws_main_settings_page')
            );
        }



        function ciws_generate_main_page() {
            if (
                isset($_GET['action'], $_GET['account_id'], ) &&
                $_GET['action'] == 'edit_account' // Verify nonce
            ) {
                if (!empty($_GET['account_id'])) {
                    $account_id = !empty($_GET['account_id']) ? absint($_GET['account_id']) : 0;
                    $account_row = $this->get_account_row_by_id($account_id);

                    if (empty($account_row)) {
                        /* translators: %d is replaced with "account id" */
                        echo sprintf(esc_html__("No account found with ID %d", 'chat-in-website'), esc_html($account_id));
                    } else {
                        $account_name = $account_row->account_name;
                        $account_id = $account_row->account_id;
                        $account_details = maybe_unserialize($account_row->ciws_account_details);
                    }
                    include CIWS_PATH . '/includes/views/backend/accounts/ciws-account-edit.php';
                }
            } else {

                include CIWS_PATH . '/includes/views/backend/accounts/ciws-account-lists.php';
            }
        }


        function ciws_add_new_account_settings_page() {
            include(CIWS_PATH . '/includes/views/backend/accounts/ciws-add-new-account.php');
        }

        function ciws_floating_icon_settings_page() {
            $ciws_floating_settings = get_option('ciws_floating_settings');
            $selected_account = (!empty($ciws_floating_settings['select_account']['ciws_floating_account_id'])) ? $ciws_floating_settings['select_account']['ciws_floating_account_id'] : '';
            $account_row = $this->get_account_row_by_id($selected_account);
            $account_details = (!empty($account_row->ciws_account_details)) ? maybe_unserialize($account_row->ciws_account_details) : [];
            
            include(CIWS_PATH . '/includes/views/backend/floating-icons/ciws-floating-icon-settings.php');
        }

        function ciws_main_settings_page() {
            include(CIWS_PATH . '/includes/views/backend/ciws-main-settings.php');
        }
    }

    new CIWS_Admin();
}
