using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Diagnostics;

public partial class Events : System.Web.UI.Page
{
    
  

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            //HttpContext.Current.Session["mode"] = "l";
            //Session["uid"] = "1";
            ExceptionUtility.LogException(0, "", "55", "Page_Load", "");
            loadEvents();

            //load the channels in the attach channel to event controls' panel
            loadChannels();
        }
        lblError.Text = "";

    }

    private void loadEvents()
    {
        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 = "RECON_EVENT_EDITOR.Get_Recon_Event";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_re_code", OracleType.Number).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_re_code"].Value = OracleNumber.Null;

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

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

            oracon.Close();
            ExceptionUtility.LogException(0, "RECON_EVENT_EDITOR.Get_Recon_Event", "", "loadEvents", "p_lang-EN");
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "RECON_EVENT_EDITOR.Get_Recon_Event", "", "loadEvents", "p_lang-EN");
            GridView1.DataSource = null;
            GridView1.DataBind();
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    private void loadChannels(string Uid)
    {
        try
        {
            string currentMethod = "";
            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 = "RECON_EVENT_EDITOR.Get_Event_Channels";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_re_code", OracleType.Number).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_re_code"].Value = Uid;

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

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

            oracon.Close();
            if (currentMethod.Contains("GridView1_RowCommand"))
                ExceptionUtility.LogException(0, "RECON_EVENT_EDITOR.Get_Event_Channels", "42.1", "loadChannels", "p_re_code- " + Uid);
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "RECON_EVENT_EDITOR.Get_Event_Channels", "42.1", "loadChannels", "p_re_code- " + Uid);
            GridView2.DataSource = null;
            GridView2.DataBind();
        }
    }

    private void loadChannels()
    {
        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_Recon_Channels";

            oracom.Parameters.Add("p_recon_channels", OracleType.Cursor).Direction = ParameterDirection.Output;

            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_Channels", "", "loadChannels", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("v"))
        {
            ExceptionUtility.LogException(0, "", "10.42", "GridView1_RowCommand.view", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string Uid = GridView1.DataKeys[index].Value.ToString();
            ViewState["eid"] = Uid;
            loadChannels(Uid);
            Notifications.showModal("modal-xl", this);
        }
    }

    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("d"))
        {
            ExceptionUtility.LogException(0, "", "10.305", "GridView2_RowCommand.Delete", "");
            int index = int.Parse(e.CommandArgument.ToString());
            string ChannelID = GridView2.DataKeys[index].Value.ToString();
            string EventID = ViewState["eid"].ToString();

            string error = "";

            bool saved = removeChannel(EventID, ChannelID, ref error);
            if (saved)
            {
                lblError.Text = "Channel Removed";
                Notifications.showMessage("Group Delete request sent.", this, Notifications.successMessage, false);
            }
            else
            {
                lblError.Text = error;
                Notifications.showMessage(error, this, Notifications.errorMessage, false);
            }
            loadChannels(EventID);
        }
    }

    
    
    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string Eventid = ViewState["eid"].ToString();
        string channelID = ddlChannels.SelectedValue;
        string error = "";
        ExceptionUtility.LogException(0, "", "1", "btnSave_Click", "");
        bool saved = attachChannel(Eventid, channelID, ddlMatchRefNumber.SelectedValue, ref error);
        if (saved)
        {
            lblError.Text = "Channel Attached";
            Notifications.showMessage("Channel Attached", this, Notifications.successMessage, true);
        }
        else
        {
            
                lblError.Text = error;
                Notifications.showMessage(error, this, Notifications.errorMessage, true);
            

        }
        
        loadChannels(Eventid);
    }
    private int loadChgID()
    {
        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 = "maker_checker_editor.get_chg_id";


            oracom.Parameters.Add("p_chg_id", OracleType.Number).Direction = ParameterDirection.Output;

            oracom.ExecuteNonQuery();
            oracon.Close();

            int nop = int.Parse(oracom.Parameters["p_chg_id"].Value.ToString());
            oracon.Close();
            return nop;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "maker_checker_editor.get_chg_id", "", "loadChgID", "");
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }

    private int loadMaxChgID(string atm_number)
    {
        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 = "maker_checker_editor.get_max_chg_id";

            oracom.Parameters.Add("p_changed_rec_id", OracleType.VarChar).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_chg_id", OracleType.Number).Direction = ParameterDirection.Output;

            oracom.Parameters["p_changed_rec_id"].Value = atm_number;

            oracom.ExecuteNonQuery();
            oracon.Close();

            int nop = int.Parse(oracom.Parameters["p_chg_id"].Value.ToString());
            oracon.Close();
            return nop;
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "maker_checker_editor.get_max_chg_id", "", "loadMaxChgID", "p_changed_rec_id- " + atm_number);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return 0;
        }
    }

    private bool attachChannel(string eventID, string channelID, string refNumber, ref string error)
    {
        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 = "RECON_EVENT_EDITOR.Edit_Event_Channels";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_re_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_rc_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_ref_num", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_action", 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_re_code"].Value = eventID;
            oracom.Parameters["p_rc_code"].Value = channelID;
            oracom.Parameters["p_ref_num"].Value = refNumber;
            oracom.Parameters["p_action"].Value = "A";
            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, "RECON_EVENT_EDITOR.Edit_Event_Channels", "1.1", "attachChannel", "eventID: " + eventID + ", channelID: " + channelID + ", refNumber: " + refNumber);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "RECON_EVENT_EDITOR.Edit_Event_Channels", "1.1", "attachChannel", "eventID: " + eventID + ", channelID: " + channelID + ", refNumber: " + refNumber);
                return false;
            }
        }
        catch (Exception ex)
        {
            ExceptionUtility.LogException(ex, "RECON_EVENT_EDITOR.Edit_Event_Channels", "1.1", "attachChannel", "eventID: " + eventID + ", channelID: " + channelID + ", refNumber: " + refNumber);
            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    private bool removeChannel(string eventID, string channelID, ref string error)
    {
        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 = "RECON_EVENT_EDITOR.Edit_Event_Channels";

            oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_re_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_rc_code", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_ref_num", OracleType.Number).Direction = ParameterDirection.Input;
            oracom.Parameters.Add("p_action", 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_re_code"].Value = eventID;
            oracom.Parameters["p_rc_code"].Value = channelID;
            oracom.Parameters["p_ref_num"].Value = OracleNumber.Null;
            oracom.Parameters["p_action"].Value = "D";
            oracom.Parameters["p_lang"].Value = "EN";
            oracom.Parameters["p_current_uid"].Value = "1";

            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, "RECON_EVENT_EDITOR.Edit_Event_Channels", "305.1", "removeChannel", "eventID- " + eventID + ": channelID- " + channelID);
                return true;
            }
            else
            {
                ExceptionUtility.LogException(9, "RECON_EVENT_EDITOR.Edit_Event_Channels", "305.1", "removeChannel", "eventID- " + eventID + ": channelID- " + channelID);
                return false;
            }
        }
        catch (Exception ex)
        {

            //Response.Redirect("../ErrorPages/ErrorPage.aspx");
            return false;
        }
    }

    protected void GridView1_PreRender(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count > 0)
        {
            GridView1.UseAccessibleHeader = true;
            GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
    }
}