using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.OracleClient;
using EjadaCSGenParser;
using EjadaCSLoader;
using EjadaFileLoaderLicenceVerifier;
using System.Threading;

public partial class Business_Monitor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SecurityAgent.AuthenticateArchive();
            loadJobs();
            loadJobNames();
            loadFileTypes();
            btnStart.Enabled = false;
            ExceptionUtility.LogException(0, "", "15", "Page_Load", "");
        }
        lblError.Text = "";
    }

    private void loadJobs()
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "APPLICATION_MONITOR.Get_Job_Status";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_job_type", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_output", OracleType.Cursor).Direction = ParameterDirection.Output;


            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();
            oracom.Parameters["p_job_type"].Value = "L";

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            GridView1.DataSource = ds;
            GridView1.DataBind();

            oracon.Close();
            // Label2.Text += "1 ";
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "APPLICATION_MONITOR.Get_Job_Status","", "loadJobs", "");
            GridView1.DataSource = null ;
            GridView1.DataBind();
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadJobNames()
    {
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "LOOKUP_GETTER.Get_Job_Names";

            oracom.Parameters.Add("p_job_type", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_jobs", OracleType.Cursor).Direction = ParameterDirection.Output;

            oracom.Parameters["p_job_type"].Value = "L";

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);

            ddlJobs.DataSource = ds;
            ddlJobs.DataBind();

            ddlJobs.Items.Insert(0, "------ Job Name ------");
            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Job_Names","", "loadJobNames", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadFileTypes()
    {
        try
        {
            ddlFiles.Items.Clear();
            if (ddlJobs.SelectedIndex > 0)
            {
                string DBConnection = Helper.dbConnection();

                OracleConnection oracon = new OracleConnection(DBConnection);
                oracon.Open();

                OracleCommand oracom = new OracleCommand();
                oracom.Connection = oracon;

                oracom.CommandType = CommandType.StoredProcedure;
                oracom.CommandText = "LOOKUP_GETTER.Get_Job_File_Types";

                oracom.Parameters.Add("p_job_name", OracleType.VarChar).Direction = ParameterDirection.Input;
                oracom.Parameters.Add("p_file_types", OracleType.Cursor).Direction = ParameterDirection.Output;

                oracom.Parameters["p_job_name"].Value = ddlJobs.SelectedValue;

                OracleDataAdapter da = new OracleDataAdapter(oracom);
                DataSet ds = new DataSet();
                da.Fill(ds);
                oracon.Close();
                ddlFiles.DataSource = ds;
                ddlFiles.DataBind();
            }
            ddlFiles.Items.Insert(0, "------ File Type ------");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Job_File_Types","", "loadFileTypes", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
        
    }

    private bool startJob(ref string error)
    {
        string parameters = "";
        try
        {
            string DBConnection = Helper.dbConnection();

            OracleConnection oracon = new OracleConnection(DBConnection);
            oracon.Open();

            OracleCommand oracom = new OracleCommand();
            oracom.Connection = oracon;

            oracom.CommandType = CommandType.StoredProcedure;
            oracom.CommandText = "APPLICATION_MONITOR.Start_Load_Job";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_job_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_file_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_file_type", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_all_files", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
            oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

            oracom.Parameters["p_job_name"].Value = ddlJobs.SelectedValue;
            if (ddlJobs.SelectedIndex > 0)
                parameters = "p_job_name-" + ddlJobs.SelectedValue;
            oracom.Parameters["p_file_date"].Value = DatePicker1.date;
            if (DatePicker1.date.Length > 0)
                parameters = parameters + ":p_file_date-" + DatePicker1 .date ;
            if (chkAll.Checked)
            {
                oracom.Parameters["p_file_type"].Value = OracleString.Null;
                oracom.Parameters["p_all_files"].Value = "Y";
                parameters = parameters + ":p_all_files-Y:";
            }
            else
            {
                oracom.Parameters["p_file_type"].Value = ddlFiles.SelectedValue;
                oracom.Parameters["p_all_files"].Value = "N";
                parameters = parameters + ":p_all_files-Y:p_file_type-"+ddlFiles .SelectedItem .Text ;
            }
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = Session["uid"].ToString();

            oracom.ExecuteNonQuery();
            oracon.Close();
            string a = oracom.Parameters["p_res_code"].Value.ToString();
            error = oracom.Parameters["p_res_msg"].Value.ToString();
            if (a == "0")
            {
                ExceptionUtility.LogException(0, "APPLICATION_MONITOR.Start_Load_Job", "2.1", "startJob", parameters );
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "APPLICATION_MONITOR.Start_Load_Job", "2.1", "startJob",parameters );
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "APPLICATION_MONITOR.Start_Load_Job","2.1", "startJob", parameters );
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    protected void btnStart_Click(object sender, EventArgs e)
    {
        if (DatePicker1.date.Length == 0)
        {
            lblError.Text = "please enter a date";
            ExceptionUtility.LogException(9, "", "15.2", "btnStart_Click", "");
        }
        else
        {
            if (ddlJobs.SelectedValue.Equals("DSN_Outward"))
            {
                string exe_dir = System.Configuration.ConfigurationManager.AppSettings["DinersEXEpath"];

                // New code to call dll
                string strRespMsg = "";
                EJCSLoader objEJCSLoader = new EJCSLoader();
                string parameters = exe_dir + " " + DatePicker1.date + " DINERS ";
                objEJCSLoader.Load_CS(exe_dir, DatePicker1.date, "DINERS", out strRespMsg);
                parameters += strRespMsg;
                lblError.Text = strRespMsg;
            }
            else if (ddlJobs.SelectedValue.Equals("LOAD_INFONET_WIN"))
                {
                    string exe_dir = System.Configuration.ConfigurationManager.AppSettings["INFONET"];

                    // New code to call dll
                    string strRespMsg = "";
                    EJCSLoader objEJCSLoader = new EJCSLoader();
                    string parameters = exe_dir + " " + DatePicker1.date + " INFONET ";
                    objEJCSLoader.Load_CS(exe_dir, DatePicker1.date, "INFONET", out strRespMsg);
                    parameters += strRespMsg;
                    lblError.Text = strRespMsg;
                }
            else if (ddlJobs.SelectedValue.Equals("LOAD_ACF_WIN"))
            {
                string exe_dir = System.Configuration.ConfigurationManager.AppSettings["ACF"];

                // New code to call dll
                string strRespMsg = "";
                EJCSLoader objEJCSLoader = new EJCSLoader();
                string parameters = exe_dir + " " + DatePicker1.date + " ACF ";
                objEJCSLoader.Load_CS(exe_dir, DatePicker1.date, "ACF", out strRespMsg);
                parameters += strRespMsg;
                lblError.Text = strRespMsg;

                //old code calling the exe
                /*
                System.Diagnostics.Process.Start(exe_dir, " ACF " + DatePicker1.date);
                lblError.Text = "File generated successfully" + " For date: " + DatePicker1.date;
                 * */
            }
            else if (ddlJobs.SelectedValue.Equals("LOAD_MADATLF05_WIN"))
            {
                string exe_dir = System.Configuration.ConfigurationManager.AppSettings["TLF05"];

                // New code to call dll
                string strRespMsg = "";
                EJCSLoader objEJCSLoader = new EJCSLoader();
                string parameters = exe_dir + " " + DatePicker1.date + " MADATLF05 ";
                objEJCSLoader.Load_CS(exe_dir, DatePicker1.date, "MADATLF05", out strRespMsg);
                parameters += strRespMsg;
                lblError.Text = strRespMsg;

                //old code calling the exe
                /*
                System.Diagnostics.Process.Start(exe_dir, " ACF " + DatePicker1.date);
                lblError.Text = "File generated successfully" + " For date: " + DatePicker1.date;
                 * */
            }
            else
            {
                string error = "";
                ExceptionUtility.LogException(0, "", "15.2", "btnStart_Click", "");
                bool saved = startJob(ref error);
                if (saved)
                {

                    lblError.Text = "Job Started";
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        lblError.Text = "An Error Occured!";
                        ExceptionUtility.LogException(9, "", "15.2", "btnStart_Click", error);
                    }
                    else
                        lblError.Text = error;
                }
            }
            loadJobs();
        }
    }

    public void runLoader()
    {
        string parameters = "";
        try
        {
            string strRespMsg = "";

            int iRespCode = 0;



            //EjadaCSLoader.EJCSLoader objEJCSGenerator = new EjadaCSLoader.EJCSLoader();



            //iRespCode = objEJCSGenerator.GenCSOutGoing(strLogLocation, strProcDate, strCSName, out strRespMsg);
            //ExceptionUtility.LogException("loadJobs", parameters);
        }
        catch (Exception ex)
        {
            //ExceptionUtility.LogException(ex, "loadJobs", parameters);
        }
    }

    protected void ddlJobs_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadFileTypes();
        btnStart.Enabled = false;
        if (ddlJobs.SelectedValue.Equals("LOAD_B24")
            || ddlJobs.SelectedValue.Equals("LOAD_MDS")
            || ddlJobs.SelectedValue.Equals("LOAD_VISA"))
        {
            chkAll.Enabled = false;
            ddlFiles.Enabled = true;
        }
        else
        {
            chkAll.Enabled = true;
        }
        chkAll.Checked = false;
        ExceptionUtility.LogException(0, "", "", "ddlJobs_SelectedIndexChanged", "");

        if (ddlJobs.SelectedValue.Equals("DSN_Outward") || ddlJobs.SelectedValue.Equals("LOAD_INFONET_WIN") || ddlJobs.SelectedValue.Equals("LOAD_ACF_WIN"))
        {
            btnStart.Enabled = true;
            chkAll.Visible = false;
            ddlFiles.Visible = false;
        }
        else
        {
            btnStart.Enabled = false;
            chkAll.Visible = true;
            ddlFiles.Visible = true;
        }
    }
    
    protected void ddlFiles_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlFiles.SelectedIndex > 0)
            btnStart.Enabled = true;
        else
            btnStart.Enabled = false;
        DatePicker1.date = "";
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        loadJobs();
       
    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (chkAll.Checked)
        {
            ddlFiles.Enabled = false;
            if(ddlJobs.SelectedIndex > 0)
                btnStart.Enabled = true;
        }
        else
        {
            ddlFiles.Enabled = true;
            ddlFiles.SelectedIndex = 0;
            btnStart.Enabled = false;
        }
    }
}
