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;

namespace MYAPP.Manage.Project.Design.GigaMap
{
    public partial class AD_GigaMap : MyApp.Web.UI.UserPage
    {
        public string _Map_ID = "", _Tour_ID = "";
        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Tour_ID = Request.QueryString["Tour_ID"].Trim();
                _Map_ID = Request.QueryString["Map_ID"].Trim();
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
            base.AppInit();
        }

        override protected void DataInit()//数据初始化
        {
        }

        public string GetElemValueByPath(object _Tour_ID, object map_id, string attribute)
        {
            string _File_Dir = Server.MapPath(String.Format("/Project/{0}/GigaMap/{1}/scene.xml", _Tour_ID, map_id.ToString()));
            StreamReader sr = System.IO.File.OpenText(_File_Dir);
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(sr.ReadToEnd());
            sr.Close();
            XmlNode xmlnode = xmldoc.ChildNodes[0];
            XmlElement xmlelem = (XmlElement)xmlnode;
            if (xmlelem.GetAttribute(attribute).Trim() != "")
            {
                return xmlelem.GetAttribute(attribute).Trim();
            }
            else
            {
                return "　";
            }
        }

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

        protected void Page_Init(object sender, EventArgs e)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        override protected void pl_del()
        {
            try
            {
                Directory.Delete(Server.MapPath(String.Format("/Project/{0}/GigaMap/{1}", _Tour_ID, _Map_ID)), true);
                Response.Redirect("List_GigaMap.aspx?Tour_ID=" + _Tour_ID);
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
        }

        override protected void pl_edit()
        {
            XmlDocument xmldoc;
            xmldoc = MyApp.Components.KRPano.GigaMap.GetSceneXmlDoc(_Tour_ID, _Map_ID);
            try
            {
                this.ID.Text = _Map_ID;

                #region KRPano
                XmlElement elem_krpano = (XmlElement)xmldoc.GetElementsByTagName("krpano", "")[0];
                GetElemValue(elem_krpano, "onstart", this.onstart, true);
                GetElemValue(elem_krpano, "basedir", this.basedir, true);
                GetElemValue(elem_krpano, "logkey", this.logkey, true);
                GetElemValue(elem_krpano, "actionintervalbase", this.actionintervalbase, true);
                #endregion

                #region General
                XmlElement elem_Project = (XmlElement)xmldoc.GetElementsByTagName("krpano", "")[0];
                GetElemValue(elem_Project, "Name", this.Name, true);
                GetElemValue(elem_Project, "Date", this.Date, true);
                GetElemValue(elem_Project, "LockTF", this.LockTF, true);
                GetElemValue(elem_Project, "Intro", this.Intro, true);
                #endregion

                #region Set
                XmlElement elem_set = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/SSet");
                GetElemValue(elem_set, "Order", this.Order);
                GetElemValue(elem_set, "Key", this.Key);
                GetElemValue(elem_set, "SearchTF", this.SearchTF);
                #endregion

                #region BKSound
                XmlElement elem_bksound = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/BKSound");
                GetElemValue(elem_bksound, "mp3file", this.mp3file, true);
                GetElemValue(elem_bksound, "volume", this.volume, true);
                GetElemValue(elem_bksound, "pan", this.pan, true);
                GetElemValue(elem_bksound, "loops", this.loops, true);
                GetElemValue(elem_bksound, "oncomplete", this.oncomplete, true);
                #endregion

                #region View
                XmlElement elem_View = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/view");
                GetElemValue(elem_View, "fovtype", this.view_fovtype, true);
                GetElemValue(elem_View, "maxpixelzoom", this.view_maxpixelzoom, true);
                GetElemValue(elem_View, "hlookat", this.view_hlookat, true);
                GetElemValue(elem_View, "vlookat", this.view_vlookat, true);
                GetElemValue(elem_View, "fov", this.view_fov, true);
                GetElemValue(elem_View, "fovmin", this.view_fovmin, true);
                GetElemValue(elem_View, "fovmax", this.view_fovmax, true);
                GetElemValue(elem_View, "limitview", this.view_limitview, true);
                GetElemValue(elem_View, "hlookatmin", this.view_hlookatmin, true);
                GetElemValue(elem_View, "hlookatmax", this.view_hlookatmax, true);
                GetElemValue(elem_View, "vlookatmin", this.view_vlookatmin, true);
                GetElemValue(elem_View, "vlookatmax", this.view_vlookatmax, true);
                #endregion

                #region Events
                XmlElement elem_events = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/events");
                GetElemValue(elem_events, "onxmlcomplete", this.onxmlcomplete, true);
                GetElemValue(elem_events, "onpreviewcomplete", this.onpreviewcomplete, true);
                GetElemValue(elem_events, "onloadcomplete", this.onloadcomplete, true);
                GetElemValue(elem_events, "onnewpano", this.onnewpano, true);
                GetElemValue(elem_events, "onremovepano", this.onremovepano, true);
                #endregion

            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
            this.ID.Enabled = false;
        }

        protected void ButPost_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            Button bn = (Button)sender;
            if (Page.IsValid)
            {
                _Map_ID = this.ID.Text.Trim();
                try
                {
                    if (action == "edit")
                    {
                        xmldoc = MyApp.Components.KRPano.GigaMap.GetSceneXmlDoc(_Tour_ID, _Map_ID);
                    }
                    #region 赋值区域

                    #region KRPano
                    XmlElement elem_krpano = (XmlElement)xmldoc.GetElementsByTagName("krpano", "")[0];
                    SetElemValue(elem_krpano, "onstart", this.onstart);
                    SetElemValue(elem_krpano, "basedir", this.basedir);
                    SetElemValue(elem_krpano, "logkey", this.logkey);
                    SetElemValue(elem_krpano, "actionintervalbase", this.actionintervalbase);
                    #endregion

                    #region General
                    XmlElement elem_Project = (XmlElement)xmldoc.GetElementsByTagName("krpano", "")[0];
                    SetElemValue(elem_Project, "Name", this.Name);
                    SetElemValue(elem_Project, "Date", this.Date);
                    SetElemValue(elem_Project, "LockTF", this.LockTF);
                    SetElemValue(elem_Project, "Intro", this.Intro);
                    #endregion


                    #region SSet
                    XmlElement elem_sset = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/SSet");
                    SetElemValue(elem_sset, "Scene_ID", _Map_ID);
                    SetElemValue(elem_sset, "Scene_Name", this.Name);
                    SetElemValue(elem_sset, "Order", this.Order);
                    SetElemValue(elem_sset, "Key", this.Key);
                    SetElemValue(elem_sset, "SearchTF", this.SearchTF);
                    SetElemValue(elem_sset, "Intro", this.Intro);
                    #endregion

                    #region BKSound
                    XmlElement elem_bksound = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/BKSound");
                    SetElemValue(elem_bksound, "mp3file", this.mp3file);
                    SetElemValue(elem_bksound, "volume", this.volume);
                    SetElemValue(elem_bksound, "pan", this.pan);
                    SetElemValue(elem_bksound, "loops", this.loops);
                    SetElemValue(elem_bksound, "oncomplete", this.oncomplete);
                    #endregion

                    #region View
                    XmlElement elem_View = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/view");
                    SetElemValue(elem_View, "fovtype", this.view_fovtype);
                    SetElemValue(elem_View, "maxpixelzoom", this.view_maxpixelzoom);
                    SetElemValue(elem_View, "hlookat", this.view_hlookat);
                    SetElemValue(elem_View, "vlookat", this.view_vlookat);
                    SetElemValue(elem_View, "fov", this.view_fov);
                    //SetElemValue(elem_View, "fovmin", this.view_fovmin);
                    //SetElemValue(elem_View, "fovmax", this.view_fovmax);
                    SetElemValue(elem_View, "limitview", this.view_limitview);
                    //SetElemValue(elem_View, "hlookatmin", this.view_hlookatmin);
                    //SetElemValue(elem_View, "hlookatmax", this.view_hlookatmax);
                    //SetElemValue(elem_View, "vlookatmin", this.view_vlookatmin);
                    //SetElemValue(elem_View, "vlookatmax", this.view_vlookatmax);
                    #endregion

                    #region Events
                    XmlElement elem_events = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/events");
                    SetElemValue(elem_events, "onxmlcomplete", this.onxmlcomplete, true);
                    SetElemValue(elem_events, "onpreviewcomplete", this.onpreviewcomplete, true);
                    SetElemValue(elem_events, "onloadcomplete", this.onloadcomplete, true);
                    SetElemValue(elem_events, "onnewpano", this.onnewpano, true);
                    SetElemValue(elem_events, "onremovepano", this.onremovepano, true);
                    #endregion


                    #region   背景音乐设置动作
                    bool sound_tf = false;
                    string str_sound = "";
                    XmlElement ac_elem_bksound;
                    ac_elem_bksound = (XmlElement)xmldoc.SelectSingleNode("/krpano/action[@name='_playscenesound']");
                    if ((ac_elem_bksound != null) && (ac_elem_bksound.OuterXml != ""))
                    {
                    }
                    else
                    {
                        ac_elem_bksound = xmldoc.CreateElement("action");
                        ac_elem_bksound.SetAttribute("name", "_playscenesound");
                        sound_tf = true;
                    }
                    if (this.mp3file.Text.Trim() != "")
                    {
                        str_sound = string.Format("playsound2D({0}, {1}, {2}, {3}, {4}, {5});", "scenebksound", this.mp3file.Text.Trim(), this.volume.Text.Trim(), this.pan.Text.Trim(), this.loops.Text.Trim(), this.oncomplete.Text.Trim());
                    }

                    ac_elem_bksound.InnerText = str_sound.Trim();

                    if (sound_tf)
                    {
                        xmldoc.SelectSingleNode("/krpano").AppendChild(ac_elem_bksound);
                    }
                    #endregion


                    xmldoc.LoadXml(xmldoc.OuterXml.Replace("{Scene_ID}", this.ID.Text.Trim()));

                    #endregion

                    if (action == "edit")
                    {
                        try
                        {
                            if( (fu_file.PostedFile!=null)&&(fu_file.FileName!=""))
                            {
                                MyApp.Components.KRPano.Scene.UploadMapPic("b", _Tour_ID, _Map_ID);
                                XmlDocument bxmldoc = new XmlDocument();
                                bxmldoc.Load(Server.MapPath(String.Format("{0}/b.xml", String.Format(MyApp.Components.KRPano.GigaMap._Root_Dir, _Tour_ID, _Map_ID))));

                                xmldoc.SelectSingleNode("/krpano/image").InnerXml = bxmldoc.SelectSingleNode("/krpano/image").OuterXml;
                            }

                            MyApp.Components.XML.XmlDocument.WriteAllText(MyApp.Components.KRPano.GigaMap.GetSceneServerFilePath(_Tour_ID, _Map_ID), xmldoc.OuterXml);

                            try
                            {
                                System.IO.File.Delete(HttpContext.Current.Server.MapPath(String.Format("/Project/{0}/GigaMap/{1}/b.xml", _Tour_ID, _Map_ID)));
                            }
                            catch
                            { }
                            if (bn.CommandName == "ret")
                            {
                                Response.Redirect("List_GigaMap.aspx?Tour_ID=" + _Tour_ID);
                            }
                            else
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('保存成功！！！');</script>");
                            }
                        }
                        catch (Exception) { ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('Error!!!);</script>"); }
                    }
                }
                catch (Exception)
                {
                    ButPost.Enabled = false;
                    ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                    return;
                }
            }
        }
    }
}