<?php

defined('BASEPATH') or exit('No direct script access allowed');

/*
Module Name: MPC Zapier
Description: Default module to Zapier Integration by MyPerfexCrm
Version: 1.0.0
Requires at least: 2.3.*
*/

define('ZAPIER', 'zapier');

hooks()->add_action('admin_init', 'zapier_integration');
hooks()->add_action('admin_init', 'zapier_add_settings_tab');

function zapier_add_settings_tab()
{
    $CI = &get_instance();
    $CI->app_tabs->add_settings_tab('zapier', [
        'name'     => _l('zapier'),
        'view'     => ZAPIER . '/zapier',
        'position' => 101,
    ]);
}
get_instance()->load->helper(ZAPIER . '/zapier');


/**
 * Register activation module hook
 */
register_activation_hook(ZAPIER, 'zapier_module_activation_hook');

function zapier_module_activation_hook()
{
    $CI = &get_instance();
    require_once(__DIR__ . '/install.php');
}

/**
 * Register language files, must be registered if the module is using languages
 */
register_language_files(ZAPIER, [ZAPIER]);

/**
 * Init ZAPIER LEADS INTEGRATION module menu items in setup in admin_init hook
 * @return null
 */
function zapier_integration()
{
    $CI = &get_instance();

    $CI->app->add_quick_actions_link([
        'name'       => _l('zapier'),
        'permission' => 'zapier',
        'url'        => 'zapier',
        'position'   => 69,
    ]);
}


//client hooks
hooks()->add_action('after_client_added', 'callback_after_client_added');
hooks()->add_action('after_client_updated', 'callback_after_client_updated');
hooks()->add_action('before_client_deleted', 'callback_before_client_deleted');

//Leads hooks
hooks()->add_action('lead_created', 'callback_after_lead_added');
hooks()->add_action('lead_status_changed', 'callback_after_lead_updated');
hooks()->add_action('lead_marked_as_lost', 'callback_after_lead_updated');
hooks()->add_action('lead_marked_as_junk', 'callback_after_lead_updated');
hooks()->add_action('before_lead_deleted', 'callback_before_lead_deleted');

// Invoice hooks
hooks()->add_action('after_invoice_added', 'callback_after_invoice_added');
hooks()->add_action('after_invoice_updated', 'callback_after_invoice_updated');
hooks()->add_action('before_invoice_deleted', 'callback_before_invoice_deleted');

// Contract hooks
hooks()->add_action('after_contract_added', 'callback_after_contract_added');
hooks()->add_action('after_contract_updated', 'callback_after_contract_updated');
hooks()->add_action('before_contract_deleted', 'callback_before_contract_deleted');

//Estimate hooks
hooks()->add_action('after_estimate_added', 'callback_after_estimate_added');
hooks()->add_action('after_estimate_updated', 'callback_after_estimate_updated');
hooks()->add_action('before_estimate_deleted', 'callback_before_estimate_deleted');

// Ticket hooks
hooks()->add_action('ticket_created', 'callback_after_ticket_created');
hooks()->add_action('after_ticket_status_changed', 'callback_after_ticket_updated');
hooks()->add_action('after_ticket_reply_added', 'callback_after_ticket_updated');
hooks()->add_action('before_ticket_deleted', 'callback_before_ticket_deleted');

// Department hooks
hooks()->add_action('after_department_added', 'callback_after_department_added');
hooks()->add_action('before_department_updated', 'callback_before_department_updated');
hooks()->add_action('before_delete_department', 'callback_before_delete_department');

// Payment hooks
hooks()->add_action('after_payment_added', 'callback_after_payment_added');
hooks()->add_action('before_payment_updated', 'callback_before_payment_updated', 10, 2);
hooks()->add_action('before_payment_deleted', 'callback_before_payment_deleted');



hooks()->add_action('app_init', ZAPIER . '_actLib');
function zapier_actLib()
{

    $CI = &get_instance();
    $CI->load->library(ZAPIER . '/Envapi');
    $lic_manager = $CI->envapi->validatePurchase(ZAPIER);
    if (!$lic_manager) {
        set_alert('danger', "One of your modules failed its verification and got deactivated. Please reactivate or contact support.");
        redirect(admin_url('modules'));
    }
}

hooks()->add_action('pre_activate_module', ZAPIER . '_sidecheck');
function zapier_sidecheck($module_name)
{

    if ($module_name['system_name'] == ZAPIER) {
        if (!option_exists(ZAPIER . '_verified') && empty(get_option(ZAPIER . '_verified')) && !option_exists(ZAPIER . '_verification_id') && empty(get_option(ZAPIER . '_verification_id'))) {
            $CI = &get_instance();
            $data['submit_url'] = $module_name['system_name'] . '/mpc_env/activate';
            $data['original_url'] = admin_url('modules/activate/' . ZAPIER);
            $data['module_name'] = ZAPIER;
            $data['title'] = "Module activation";
            echo $CI->load->view($module_name['system_name'] . '/activate', $data, true);
            exit();
        }
    }
}

hooks()->add_action('pre_deactivate_module', ZAPIER . '_deregister');
function zapier_deregister($module_name)
{
    if ($module_name['system_name'] == ZAPIER) {
        $CI = &get_instance();
        $CI->load->library(ZAPIER . '/Envapi');
        $CI->envapi->deactivateLicense(get_option(ZAPIER . '_verification_id'));
        delete_option(ZAPIER . "_verified");
        delete_option(ZAPIER . "_verification_id");
        delete_option(ZAPIER . "_last_verification");
        delete_option(ZAPIER . "_expire_verification");
    }
}
