using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using DataAccess;
using System.Data.OracleClient;

namespace VMT
{
    public partial class MapView_PS : System.Web.UI.Page
    {
        string m_SP_MAP_SELECT_WA_EVT_CODE_DESC = Properties.Settings.Default.SP_MAP_SELECT_WA_EVT_CODE_DESC;
        string m_SP_MAP_SELECT_WA_EC_BY_LINEID = Properties.Settings.Default.SP_MAP_SELECT_WA_EC_BY_LINEID;
        string m_SP_VDM_SEL_MAP_ALL_EVENTCODE = Properties.Settings.Default.SP_VDM_SEL_MAP_ALL_EVENTCODE;
        string m_SP_VDM_SEL_MAP_WORKAREA = Properties.Settings.Default.SP_VDM_SEL_MAP_WORKAREA;
        string m_SP_MAP_SELECT_EC_MIDPOINT = Properties.Settings.Default.SP_MAP_SELECT_EC_MIDPOINT;
       // string m_SP_MAP_SELECT_EC_MIDPOINT = "SP_MAP_SELECT_EC_MIDPOINT";
       public double zlat = 33.9897;
       public  double zlong = -81.0324;
        protected void Page_Load(object sender, EventArgs e)
        {
            int V_EVENT_CODE_ID = Convert.ToInt32(Request.QueryString["EVENT_CODE_ID"].ToString());
            int V_WORK_AREA_ID ;
            if (!Page.IsPostBack)
            {
                bindAllEventCode();
                if (!String.IsNullOrEmpty(Request.QueryString["EVENT_CODE_ID"]))
                {
                    ddl_EventCode.SelectedValue = Request.QueryString["EVENT_CODE_ID"];
                }
                bindWorkarea();


                if (!String.IsNullOrEmpty(Request.QueryString["WORK_AREA_ID"]))
                {
                    //lblWa.Visible = true;
                    //lblWaValue.Visible = true;
                    V_WORK_AREA_ID = Convert.ToInt32(Request.QueryString["WORK_AREA_ID"].ToString());
                    //GetEventCodeDesc(V_WORK_AREA_ID);
                    ddl_WorkArea.SelectedValue = Request.QueryString["WORK_AREA_ID"];
                }
            }


            V_EVENT_CODE_ID = Convert.ToInt32(ddl_EventCode.SelectedValue.ToString());
               GetEventCodeDescLatlink(V_EVENT_CODE_ID);


            //if (!String.IsNullOrEmpty(Request.QueryString["WORK_AREA_ID"]))
            //{
            //    //lblWa.Visible = true;
            //    //lblWaValue.Visible = true;
            //  V_WORK_AREA_ID = Convert.ToInt32(Request.QueryString["WORK_AREA_ID"].ToString());
            //    //GetEventCodeDesc(V_WORK_AREA_ID);
            //  ddl_WorkArea.SelectedValue = Request.QueryString["WORK_AREA_ID"];
            //}
            //else if (!String.IsNullOrEmpty(Request.QueryString["LINE_ID"]))
            //{
            //   // lblWa.Visible = true;
            //  //  lblWaValue.Visible = true;
            //    if (Request.QueryString["LINE_ID"] == "Summary")
            //    {
            //         V_WORK_AREA_ID = Convert.ToInt32(Request.QueryString["WkID"].ToString());
            //        GetEventCodeDesc(V_WORK_AREA_ID);
            //    }
            //    else
            //    {
            //        int V_LINE_ID = Convert.ToInt32(Request.QueryString["LINE_ID"].ToString());
            //        GetEventCodeDescByLine(V_LINE_ID);
            //    }
            //}
            ////else if (!String.IsNullOrEmpty(Request.QueryString["EVENT_CODE_ID"]))
            ////{
            ////    lblWa.Visible = false;
            ////    lblWaValue.Visible = false;
            ////    int V_EVENT_CODE_ID = Convert.ToInt32(Request.QueryString["EVENT_CODE_ID"].ToString());
            ////    GetEventCodeDescLatlink(V_EVENT_CODE_ID);
            ////}
            //else
            //{
            //   // lblWa.Visible = false;
            //  //  lblWaValue.Visible = false;
            //    return;
            //}


        }

        void bindAllEventCode()
        {
            try
            {
                DataAccessUtility.BindDropdownListWithoutConditionViaProcedure(m_SP_VDM_SEL_MAP_ALL_EVENTCODE, ddl_EventCode);

            }
            catch
            {
            }
        }

        void bindWorkarea()
        {

           
            int V_EVENT_CODE_ID = 0;
            try
            {
                V_EVENT_CODE_ID = Convert.ToInt32(ddl_EventCode.SelectedValue);
                List<OleDbParameter> OledbParams = new List<OleDbParameter>();
                OledbParams.Add(new OleDbParameter("V_EVENT_CODE_ID", V_EVENT_CODE_ID));
                DataAccessUtility.BindDropdownListWithConditionViaProcedure(m_SP_VDM_SEL_MAP_WORKAREA, OledbParams, ddl_WorkArea);
            }
            catch
            {
            }
        }
        private void GetEventCodeDescLatlink(int V_EVENT_CODE_ID)
        {
            DataSet ds = new DataSet();
            List<OleDbParameter> OleDbParams = new List<OleDbParameter>();
            OleDbParams.Add(new OleDbParameter("V_EVENT_CODE_ID", V_EVENT_CODE_ID));

            ds = DataAccessUtility.SelectDataSetViaStoreProcedureParam(m_SP_MAP_SELECT_EC_MIDPOINT, OleDbParams);
            if (ds.Tables[0].Rows.Count > 0)
            {
                zlat = Convert.ToDouble(ds.Tables[0].Rows[0][0].ToString());
                zlong = Convert.ToDouble(ds.Tables[0].Rows[0][1].ToString());
            }

            //OleDbConnection OracleConn;

            //// Get the connection string from the web.config file.
            //string myConnectionString = ConfigurationManager.ConnectionStrings["OracleConnectionString"].ConnectionString;

            //// Create a new SQL Connection using that connection string.
            //OracleConn = new OleDbConnection(myConnectionString);


            //using (OracleConn)
            //using (var cmd = OracleConn.CreateCommand())
            //{
            //    OracleConn.Open();
            //    cmd.CommandText = "select sde.st_y(SDE.st_geometry_operators.st_transform_f(SDE.st_point(centerx, centery, 2273), 4269)) as lat_y, sde.st_x(SDE.st_geometry_operators.st_transform_f(SDE.st_point(centerx, centery, 2273), 4269)) as long_x from ( select (maxx + minx) / 2 as centerx, (maxy + miny) / 2 as centery from ( select max(sde.st_geometry.st_maxx(shape)) as maxx, max(sde.st_geometry.st_maxy(shape)) as maxy ,min(sde.st_geometry.st_minx(shape)) as minx, min(sde.st_geometry.st_miny(shape)) as miny  from vegdbo.trim_lines where event_code_id = " + V_EVENT_CODE_ID + " ))";
            //    using (var reader = cmd.ExecuteReader())
            //    {
                   
                    
                    
            //        while (reader.Read())
            //        {

            //            zlat = Convert.ToDouble(reader.GetValue(0));
            //            zlong = Convert.ToDouble(reader.GetValue(1));

            //        }
            //    }
            //}


        }
        private void GetEventCodeDesc(int V_WORK_AREA_ID)
        {
            DataSet ds = new DataSet();
            List<OleDbParameter> OleDbParams = new List<OleDbParameter>();
            OleDbParams.Add(new OleDbParameter("V_WORK_AREA_ID", V_WORK_AREA_ID));

            ds = DataAccessUtility.SelectDataSetViaStoreProcedureParam(m_SP_MAP_SELECT_WA_EVT_CODE_DESC, OleDbParams);
            if (ds.Tables[0].Rows.Count > 0)
            {
                hdnEventCode.Value = ds.Tables[0].Rows[0][0].ToString();
                hdnWworkArea.Value = ds.Tables[0].Rows[0][1].ToString();
            }
        }

        private void GetEventCodeDescByLine(int V_LINE_ID)
        {
            int V_WkID = Convert.ToInt32(Request.QueryString["WkID"].ToString());
            DataSet ds = new DataSet();
            List<OleDbParameter> OleDbParams = new List<OleDbParameter>();
            OleDbParams.Add(new OleDbParameter("V_LINE_ID", V_LINE_ID));
            OleDbParams.Add(new OleDbParameter("V_WkID", V_WkID));
            ds = DataAccessUtility.SelectDataSetViaStoreProcedureParam(m_SP_MAP_SELECT_WA_EC_BY_LINEID, OleDbParams);
            if (ds.Tables[0].Rows.Count > 0)
            {
                hdnEventCode.Value = ds.Tables[0].Rows[0][0].ToString();
                hdnWworkArea.Value = ds.Tables[0].Rows[0][1].ToString();
            }
        }

        protected void ddl_EventCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindWorkarea();
        }

        protected void ddl_EventCode_DataBound(object sender, EventArgs e)
        {
            bindWorkarea();
        }

    }
}