<?php
class agile_tix_Widget_Active{
    //this needs to be hooked into the admin and public head
    //add_action('admin_head','agile_echo_head');
    //add_action('wp_head','agile_echo_head');
    function agile_echo_head(){


        //agile_tix::agile_tix_log_event('agile_echo_head called! Current action: ' . current_action());

        $activate = get_option('agile_field_widget_active') ?? false;
        
        if (current_action() == "wp_head"){
            //agile_tix::agile_tix_log_event('agile_echo_head run $activate ' . $activate);
        }
        
        if ( isset($activate) && $activate != false){

            // agile_tix::agile_tix_log_event('agile_echo_head if == true ' . $activate);

            $webURL = get_option('agile_field_webURL');
            $corpOrgID = get_option('agile_field_corpOrgID');
            $epgID = get_option('agile_field_epgID');

            /*
            wp_cache_delete('agile_field_widget_target', 'options');
            wp_cache_delete('agile_field_widget_mode', 'options');
            */


            $target = get_option('agile_field_widget_target') ?? 'agile_frame';
            $mode = get_option('agile_field_widget_mode') ?? 'popup';
            /*
            if ($target == ""){
                $target = 'agile_frame';
            }

            if ($mode == ""){
                $mode = 'popup';
            }
            */

            //if (current_action() == "wp_head"){
                //agile_tix::agile_tix_log_event('agile_verify_credentials_after_save run $webURL ' . $webURL);
                //agile_tix::agile_tix_log_event('agile_verify_credentials_after_save run $corpOrgID ' . $corpOrgID);
                //agile_tix::agile_tix_log_event('agile_verify_credentials_after_save run $epgID ' . $epgID);
                //agile_tix::agile_tix_log_event('agile_verify_credentials_after_save run $target ' . $target);
                //agile_tix::agile_tix_log_event('agile_verify_credentials_after_save run $mode ' . $mode);
            //}


            //agile_tix::agile_tix_log_event('agile_verify_credentials_after_save run $target ' . $target);
            //agile_tix::agile_tix_log_event('agile_verify_credentials_after_save run $corpOrgID ' . $corpOrgID);
            //agile_tix::agile_tix_log_event('agile_verify_credentials_after_save run $epgID ' . $epgID);
            //agile_tix::agile_tix_log_event('agile_verify_credentials_after_save run $target ' . $target);
            //agile_tix::agile_tix_log_event('agile_verify_credentials_after_save run $mode ' . $mode);

            
            if ($webURL != '' && $corpOrgID != '' && $epgID != '' && $target != '' && $mode != '' ){
                $sourceString = $webURL . 'agile_integrate.ashx?orgid=' . $corpOrgID . '&epgid=' . $epgID . '&target=' . $target .  '&mode=' . $mode . '&';    
                //agile_tix::agile_tix_log_event('agile_verify_credentials_after_save run $sourceString ' . $sourceString);
                echo '<script type="text/javascript" src="' . $sourceString .'"></script>';
            }
        }
    }
}