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 System.Diagnostics;

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

    private void loadJobs()
    {
        string currentMethod = "";
        try
        {
            StackTrace stackTrace = new StackTrace();
            currentMethod = stackTrace.GetFrame(1).GetMethod().Name;
            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 = "D";

            OracleDataAdapter da = new OracleDataAdapter(oracom);
            DataSet ds = new DataSet();
            da.Fill(ds);
            //if (ds.Tables.Count > 0)
            //{
            //    for (int i = 0; i < ds.Tables.Count; i++)
            //    {
            //        if (ds.Tables[0].Rows[i]["status"].ToString().Equals("COMPLETED"))
            //        {
            //        }
            //    }
            //}
            GridView1.DataSource = ds;
            GridView1.DataBind();

            oracon.Close();
            // Label2.Text += "1 ";
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "APPLICATION_MONITOR.Get_Job_Status","", "loadJobs", "");
            //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 = "D";

            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 loadChannels()
    {
        try
        {
            ddlChannels.Items.Clear();
            string DBConnection = Helper.dbConnection();

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

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

            oracom.CommandType = CommandType.StoredProcedure;
            if (ddlJobs.SelectedValue == "REV_RECON")
            {//revert reconciliation
                oracom.CommandText = "LOOKUP_GETTER.Get_Recon_Job_Channels";

                oracom.Parameters.Add("p_recon_channels", OracleType.Cursor).Direction = ParameterDirection.Output;
                ddlChannels.DataTextField = "channel_name";
                ddlChannels.DataValueField = "rc_code";

            }
            else
            {// unload file
                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";

                ddlChannels.DataTextField = "display_name";
                ddlChannels.DataValueField = "job_name";
            }

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

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

            ddlChannels.Items.Insert(0, "------ Channels ------");
            oracon.Close();
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Recon_Job_Channels","", "loadChannels", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadChannelEvents()
    {

        try
        {
            ddlEvents.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;
                if (ddlJobs.SelectedValue == "REV_RECON")
                {
                    oracom.CommandText = "LOOKUP_GETTER.get_revert_job_file_types";

                    oracom.Parameters.Add("p_channel_name", OracleType.VarChar).Direction = ParameterDirection.Input;
                    oracom.Parameters.Add("p_file_types", OracleType.Cursor).Direction = ParameterDirection.Output;
                   // string dataValueField = ddlChannels.DataValueField;
                    oracom.Parameters["p_channel_name"].Value = ddlChannels.SelectedValue;
                    ddlEvents.DataTextField = "file_name";
                    ddlEvents.DataValueField = "file_name";

                }

                else
                {
                    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 = ddlChannels.SelectedValue;
                    ddlEvents.DataTextField = "file_type";
                    ddlEvents.DataValueField = "file_type";
                }
                OracleDataAdapter da = new OracleDataAdapter(oracom);
                DataSet ds = new DataSet();
                da.Fill(ds);
                oracon.Close();
                ddlEvents.DataSource = ds;
                ddlEvents.DataBind();
            }
            ddlEvents.Items.Insert(0, "------ File Type ------");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.Get_Job_File_Types", "", "loadFileTypes", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
        
        //try
        //{
        //    ddlEvents.Items.Clear();
        //    if (ddlChannels.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_Recon_Job_Channel_Events";

        //        oracom.Parameters.Add("p_rc_code", OracleType.VarChar).Direction = ParameterDirection.Input;
        //        oracom.Parameters.Add("p_recon_events", OracleType.Cursor).Direction = ParameterDirection.Output;

        //        oracom.Parameters["p_rc_code"].Value = ddlChannels.SelectedValue;

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

    private string getEventByFileName(string strFileName)
    {

      
        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_event_by_filename";

                oracom.Parameters.Add("p_file_name", OracleType.VarChar).Direction = ParameterDirection.Input;
                oracom.Parameters.Add("p_recon_event", OracleType.Cursor).Direction = ParameterDirection.Output;
                    // string dataValueField = ddlChannels.DataValueField;
                oracom.Parameters["p_file_name"].Value = strFileName;
                

               
                OracleDataAdapter da = new OracleDataAdapter(oracom);
                DataSet ds = new DataSet();
                da.Fill(ds);
                oracon.Close();
                return ds.Tables[0].Rows[0][0].ToString();
            
            }
            
        
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.get_event_by_filename", "", "getEventByFileName", "");
            return "0";
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }


    private string getChannelByFileName(string strFileName)
    {


        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_channel_by_filename";

            oracom.Parameters.Add("p_file_name", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_recon_channel", OracleType.VarChar,100).Direction = ParameterDirection.Output;
            // string dataValueField = ddlChannels.DataValueField;
            oracom.Parameters["p_file_name"].Value = strFileName;



            oracom.ExecuteNonQuery();
            oracon.Close();
            string strChannel = oracom.Parameters["p_recon_channel"].Value.ToString();

            return strChannel;

        }


        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "LOOKUP_GETTER.get_channel_by_filename", "", "getChannelByFileName", "");
            return "0";
            //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_Revert_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_rc_code", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_re_code", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_recon_date", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_all_events", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_file_type", OracleType.VarChar).Direction = ParameterDirection.Input; //UMER_MOD 20140917 
            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_recon_date"].Value = DatePicker1.date;
            if (DatePicker1.date.Length > 0)
                parameters = parameters + ":p_recon_date-" + DatePicker1.date;

            if (ddlJobs.SelectedValue == "DEL_FILE")
            {
                oracom.Parameters["p_rc_code"].Value = getChannelByFileName(ddlEvents.SelectedItem.Text);
                if (ddlChannels.SelectedIndex > 0)
                    parameters = parameters + ":p_rc_code-" + getChannelByFileName(ddlEvents.SelectedItem.Text);
            }
            else
            {

                oracom.Parameters["p_rc_code"].Value = ddlChannels.SelectedValue;
                if (ddlChannels.SelectedIndex > 0)
                    parameters = parameters + ":p_rc_code-" + ddlChannels.SelectedItem.Text;
            }
            
            //oracom.Parameters["p_re_code"].Value = ddlEvents.SelectedValue;
            oracom.Parameters["p_re_code"].Value = getEventByFileName(ddlEvents.SelectedItem.Text);
            
            if (ddlEvents.SelectedIndex > 0)
                parameters = parameters + ":p_re_code-" + getEventByFileName(ddlEvents.SelectedItem.Text);//ddlEvents.SelectedItem.Text;
            oracom.Parameters["p_all_events"].Value = OracleString.Null;

            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_file_type"].Value = ddlEvents.SelectedItem.Text;//UMER_MOD 20140917
            parameters = parameters + ":p_file_type-" + ddlEvents.SelectedItem.Text;//UMER_MOD 20140917
            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_Revert_Job", "33.1", "startJob", parameters);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "APPLICATION_MONITOR.Start_Revert_Job", "33.1", "startJob", parameters);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "APPLICATION_MONITOR.Start_Revert_Job","33.1", "startJob",parameters );
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    protected void btnStart_Click(object sender, EventArgs e)
    {
        if (ddlJobs.SelectedIndex == 0)
        {
            lblError.Text = "please choose a job";
            ExceptionUtility.LogException(9, "", "19.33", "startJob", "");
        }
        else if (ddlChannels.SelectedIndex == 0)
        {
            lblError.Text = "please choose a channel";
            ExceptionUtility.LogException(9, "", "19.33", "startJob", "");
        }
        else if (ddlEvents.SelectedIndex == 0)
        {
            lblError.Text = "please choose an event";
            ExceptionUtility.LogException(9, "", "19.33", "startJob", "");
        }
        else if (DatePicker1.date.Length == 0)
        {
            lblError.Text = "please enter a date";
            ExceptionUtility.LogException(9, "", "19.33", "startJob", "");
        }
        else
        {
            string error = "";
            ExceptionUtility.LogException(0, "", "19.33", "startJob", "");
            bool saved = startJob(ref error);
            if (saved)
            {
                lblError.Text = "Job Reverting Began...";
            }
            else
            {
                if (Helper.OracleError(error))
                {
                    lblError.Text = "An Error Occured!";
                    ExceptionUtility.LogException(9, "", "19.33", "btnStart_Click", error);
                }
                else
                    lblError.Text = error;
            }
            loadJobs();

            //lblError.Text = "";
        }
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        loadJobs();
        lblError.Text = "";
    }

    protected void ddlChannels_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadChannelEvents();
        ExceptionUtility.LogException(9, "", "", "ddlChannels_SelectedIndexChanged", "");
    }
    protected void ddlJobs_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadChannels();
    }
}
