using System;
using System.IO;
using System.Collections.Generic;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyApp.Components.KRPano;

namespace MYAPP.Manage.Project.Design.Scene
{
    public partial class AD_AutoHotspot : MyApp.Web.UI.UserPage
    {
        public string _Scene_ID = "", _Tour_ID = "";
        public int _ROW = 0;
        public string _filename = "/Project/{0}/scene/{1}/scene.xml";
        XmlDocument xmldoc_SceneList = new XmlDocument();
        XmlDocument xmldoc;
        double cam_height = 2;
        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Tour_ID = Request.QueryString["Tour_ID"].Trim();
                _Scene_ID = Request.QueryString["Scene_ID"].Trim();
                _ROW = Convert.ToInt32(Request["ROW"]);
            }
            catch (Exception)
            {
                return;
            }
            base.AppInit();
        }

        override protected void DataInit()//数据初始化
        {
            xmldoc_SceneList = MyApp.Components.XML.XmlDocument.Load(String.Format("/Project/{0}/ETXml/SceneList.xml", _Tour_ID));

            try
            {
                cam_height = Convert.ToDouble(MyApp.Components.KRPano.Project.Get_ProjectNodeConfigValue(_Tour_ID, "/Project/Google", "height"));
            }
            catch (Exception) { cam_height = 1; }
        }

        protected override void pl_register_script()
        {
            base.pl_register_script();
        }

        protected void del_autohotspot(XmlDocument xmldoc, string _Tour_ID, string _Scene_ID)
        {

            XmlNode xmlnode = xmldoc.SelectSingleNode("krpano");
            XmlNodeList xmlnodelist = xmldoc.SelectNodes("/krpano/hotspot[@T_AutoTF='True']");
            try
            {
                foreach (XmlElement xmlelem in xmlnodelist)
                {
                    xmlnode.RemoveChild(xmlelem);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        void add_autohotspot(XmlDocument xmldoc, string _Tour_ID, string _Scene_ID)
        {
            Double google_lng, google_lat, google_heading, google_height;

            try
            {
                XmlElement elem_Google = (XmlElement)xmldoc.SelectSingleNode("/krpano/Google");

                google_lng = Convert.ToDouble(elem_Google.GetAttribute("lng"));
                google_lat = Convert.ToDouble(elem_Google.GetAttribute("lat"));
                google_heading = Convert.ToDouble(elem_Google.GetAttribute("heading"));
                google_height = Convert.ToDouble(elem_Google.GetAttribute("height"));
            }
            catch (Exception) { Response.Write(_Scene_ID.ToString()+"<br/>"); return; }
            
            #region //循环检测场景
            XmlNodeList xmlnodelist = xmldoc_SceneList.SelectNodes("/Tour/Scene/sceneitem");
            foreach (XmlElement xmlelem in xmlnodelist)
            {
                if ((xmlelem.GetAttribute("sceneid").Trim() != _Scene_ID.Trim()) && (xmlelem.GetAttribute("GoogleMapTF").Trim() != "False"))
                {
                    Double to_lng, to_lat, to_heading, to_height;
                    double ab_distance = int.MaxValue;
                    string to_sceneid = "";
                    try
                    {
                        to_sceneid = xmlelem.GetAttribute("sceneid");
                        to_lng = Convert.ToDouble(xmlelem.GetAttribute("lng"));
                        to_lat = Convert.ToDouble(xmlelem.GetAttribute("lat"));
                        to_heading = Convert.ToDouble(xmlelem.GetAttribute("heading"));
                        to_height = Convert.ToDouble(xmlelem.GetAttribute("height"));
                    }
                    catch (Exception) {return; }
                    ab_distance = ScenceFunc.ab_distance(google_lng, google_lat, to_lng, to_lat);
                    if (ab_distance < 80)//判断两点之间的距离小于多少M
                    {
                        double _ath, _atv = 0; double temo_ath = 0;

                        temo_ath = Math.Atan(ScenceFunc.ab_distance(google_lng, google_lat, google_lng, to_lat) / ScenceFunc.ab_distance(google_lng, google_lat, to_lng, google_lat)) * 180 / Math.PI;
                        _ath = ScenceFunc.v(google_lng, google_lat, to_lng, to_lat, temo_ath) - google_heading+90;
                        //_atv = Math.Atan(-(to_height - cam_height - google_height) / ab_distance) * 180 / Math.PI;

                        _atv = Math.Atan(cam_height / ab_distance) * 180 / Math.PI;

                        #region
                        XmlElement elem_hotspot;
                        elem_hotspot = xmldoc.CreateElement("hotspot");//创建一个热点

                        SetElemValue(elem_hotspot, "T_Type", "Image");
                        SetElemValue(elem_hotspot, "url", "%FIRSTXML%/images/h.png");
                        SetElemValue(elem_hotspot, "ath", _ath.ToString("F2"));
                        SetElemValue(elem_hotspot, "atv", _atv.ToString("F2"));

                        SetElemValue(elem_hotspot, "visible", "true");
                        SetElemValue(elem_hotspot, "scale", ((100 - ab_distance) / 100/2).ToString());
                        SetElemValue(elem_hotspot, "smoothing", "true");
                        SetElemValue(elem_hotspot, "distorted", "true");
                        SetElemValue(elem_hotspot, "rotatewithview", "true");

                        SetElemValue(elem_hotspot, "onclick", String.Format("_loadstreetscene({0},{1},{2},{3});", to_sceneid, _ath.ToString("F2"), _atv.ToString("F2"), (_ath + google_heading - to_heading).ToString("F2")));

                        SetElemValue(elem_hotspot, "T_AutoTF", "True");
                        SetElemValue(elem_hotspot, "onhover","showtext(get(T_Hint));");

                        SetElemValue(elem_hotspot, "onover", "_hide_hotcursor();");
                        SetElemValue(elem_hotspot, "onout", "_show_hotcursor();");

                        SetElemValue(elem_hotspot, "lng", to_lng.ToString());
                        SetElemValue(elem_hotspot, "lat", to_lat.ToString());
                        SetElemValue(elem_hotspot, "distance", Math.Round(ab_distance, 1).ToString());
                        SetElemValue(elem_hotspot, "T_Hint", String.Format("距离:{1}m", xmlelem.GetAttribute("title"), Math.Round(ab_distance, 1)));

                        xmldoc.SelectSingleNode("/krpano").AppendChild(elem_hotspot);//添加热点
                        #endregion
                    }
                }
            }
            #endregion
        }
        protected void Page_Init(object sender, EventArgs e)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            XmlNodeList xmlnodelist = xmldoc_SceneList.SelectNodes("/Tour/Scene/sceneitem");
            foreach (XmlElement xmlelem in xmlnodelist)
            {
                string _Scene_ID = xmlelem.GetAttribute("sceneid");
                if (xmlelem.GetAttribute("GoogleMapTF").Trim() == "True")
                {
                    xmldoc = MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, _Scene_ID);
                    del_autohotspot(xmldoc, _Tour_ID, _Scene_ID);
                    add_autohotspot(xmldoc, _Tour_ID, _Scene_ID);
                    MyApp.Components.XML.XmlDocument.WriteAllText(MyApp.Components.KRPano.Scene.GetSceneServerFilePath(_Tour_ID, _Scene_ID), xmldoc.OuterXml);
                }
            }
            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('热点添加完成!!!');window.close();</script>");
        }
    }
}
