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.Scene
{
    public partial class ADDS_Scene : MyApp.Web.UI.UserPage
    {
        public string _Scene_ID = "", _Tour_ID = "", _TypeID = "";
        public string _filename = "/Project/{0}/scene/{1}/scene.xml";
        string _TourTemplates_Dir = "/Data/Templates/Scene";
        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _TypeID = Request.QueryString["TypeID"].Trim();
            }
            catch (Exception) { _TypeID = ""; }

            if (_TypeID == "-1") { _TypeID = ""; }
            try
            {
                _Tour_ID = Request.QueryString["Tour_ID"].Trim();
                _Scene_ID = Request.QueryString["Scene_ID"].Trim();
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }

            if (MyApp.Components.KRPano.Project.GetTourSceneCount(String.Format("{0}/Scene", _Tour_ID)) >= COE.Components.MyFun.GetNumber())
            {
                ButPost.Enabled = false;
                if (COE.Components.MyFun.GTX() != "localhost")
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('对不起,你所使用的版本(KRPanoGUI" + COE.Components.MyFun.bb[COE.Components.MyFun.GetNumber().ToString()] + ")最多只能添加" + COE.Components.MyFun.GetNumber().ToString() + "个场景!!!');</script>");
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('对不起,你所使用的版本(KRPanoGUI测试版)最多只能添加" + COE.Components.MyFun.GetNumber().ToString() + "个场景!!!');</script>");
                }
                return;
            }
            base.AppInit();
        }

        override protected void DataInit()//数据初始化
        {
            #region //填充模板列表
            MyApp.Components.Accessories acc = new MyApp.Components.Accessories();
            DataTable dt = acc.GetFiles(Server.MapPath(_TourTemplates_Dir), "*.xml");
            foreach (DataRow dr in dt.Rows)
            {
                this.ddl_Template.Items.Add(new ListItem(dr["File_Name"].ToString(), dr["File_Path"].ToString()));
            }
            #endregion
        }

        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_add()
        {
            this.Date.Text = System.DateTime.Now.ToShortDateString();
        }

        protected void ButPost_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            if (Page.IsValid)
            {
                 DirectoryInfo di = new DirectoryInfo(Server.MapPath("/AScene"));
                 if (di.Exists)
                 {
                     string _Name = "";
                     foreach (FileInfo fi in di.GetFiles("*.jpg"))
                     {

                         if (fi.Name.IndexOf("@") > 0)
                         {
                             _Scene_ID = fi.Name.Split('@')[0];
                             _Name = fi.Name.Split('@')[1].Replace(".jpg","");
                         }
                         else
                         {
                             _Scene_ID = fi.Name.Replace(".jpg", "");
                             _Name = "场景_" + fi.Name.Replace(".jpg", "");
                         }

                         try
                         {
                             if (action == "add")
                             {
                                 if (Directory.Exists(Server.MapPath(String.Format("/Project/{0}/Scene/{1}", _Tour_ID, _Scene_ID))))
                                 {
                                     ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('" + _Scene_ID + " ID Existed!!!');top.updatescenetree();top.getmainFrame().location='List_TreeScene.aspx?Tour_ID=" + _Tour_ID + "&TypeID=" + _TypeID + "';top.close_dialog();</script>");
                                     return;
                                 }
                                 xmldoc = MyApp.Components.XML.XmlDocument.Load("/Data/Templates/Scene/" + this.ddl_Template.SelectedValue.Trim());
                             }

                             #region 赋值区域

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


                             #region SSet
                             XmlElement elem_Sset = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/SSet");
                             SetElemValue(elem_Sset, "Scene_ID", _Scene_ID);
                             SetElemValue(elem_Sset, "Scene_Name", _Scene_ID);
                             SetElemValue(elem_Sset, "TypeID", _TypeID);
                             #endregion

                             #endregion

                             if (action == "add")
                             {
                                 try
                                 {
                                     Directory.CreateDirectory(Server.MapPath(String.Format("/Project/{0}/scene/{1}", _Tour_ID, _Scene_ID)));
                                     MyApp.Components.KRPano.Scene.UploadScenePic("b", _Tour_ID, _Scene_ID,fi.FullName);
                                     MyApp.Components.XML.XmlDocument.WriteAllText(MyApp.Components.KRPano.Scene.GetSceneServerFilePath(_Tour_ID, _Scene_ID), xmldoc.OuterXml);

                                     //MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format("/Project/{0}/scene/{1}/include_scene.xml", _Tour_ID, _Scene_ID)), xmldoc.ChildNodes[0].InnerXml);
                                 }
                                 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('错误!!!');</script>");
                             return;
                         }
                     }
                 }

                 ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>top.updatescenetree();top.getmainFrame().location='List_TreeScene.aspx?Tour_ID=" + _Tour_ID + "&TypeID=" + _TypeID + "';top.close_dialog();</script>");
            }
        }


        protected void ButPostStreet_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            if (Page.IsValid)
            {
                DirectoryInfo di = new DirectoryInfo(Server.MapPath("/SScene"));
                if (di.Exists)
                {
                    string _Name = "";
                    foreach (DirectoryInfo i_di in di.GetDirectories())
                    {
                        if (i_di.Name.IndexOf("@") > 0)
                        {
                            _Scene_ID = i_di.Name.Split('@')[0];
                            _Name = i_di.Name.Split('@')[1];
                        }
                        else
                        {
                            _Scene_ID = i_di.Name;
                            _Name = "场景_"+i_di.Name;
                        }
                        FileInfo fi = new FileInfo(Server.MapPath(string.Format("/SScene/{0}/{0}.jpg", _Scene_ID)));

                        XmlDocument xmldoc_set = new XmlDocument();
                        xmldoc_set.Load(Server.MapPath(string.Format("/SScene/{0}/set.xml", _Scene_ID)));

                        try
                        {
                            if (action == "add")
                            {
                                if (Directory.Exists(Server.MapPath(String.Format("/Project/{0}/Scene/{1}", _Tour_ID, _Scene_ID))))
                                {
                                    ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('" + _Scene_ID + " ID Existed!!!');top.updatescenetree();top.getmainFrame().location='List_TreeScene.aspx?Tour_ID=" + _Tour_ID + "&TypeID=" + _TypeID + "';top.close_dialog();</script>");
                                    return;
                                }
                                xmldoc = MyApp.Components.XML.XmlDocument.Load("/Data/Templates/Scene/" + this.ddl_Template.SelectedValue.Trim());
                            }
                            XmlElement g_set = (XmlElement)xmldoc_set.SelectSingleNode("/krpanogui/set");

                            #region 赋值区域

                            #region General
                            XmlElement elem_Project = (XmlElement)xmldoc.GetElementsByTagName("krpano", "")[0];
                            SetElemValue(elem_Project, "TypeID", _TypeID);
                            SetElemValue(elem_Project, "Name", _Name);
                            SetElemValue(elem_Project, "Date", this.Date);
                            SetElemValue(elem_Project, "ShotDate", g_set.GetAttribute("date"));
                            SetElemValue(elem_Project, "GoogleMapTF", "True");
                            #endregion

                            #region Set
                            XmlElement elem_set = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/SSet");
                            SetElemValue(elem_set, "Scene_ID", _Scene_ID);
                            SetElemValue(elem_set, "Scene_Name", _Scene_ID);
                            SetElemValue(elem_set, "TypeID", _TypeID);
                            SetElemValue(elem_set, "GoogleMapTF", "True");
                            #endregion


                            #region Google

                            XmlElement elem_Google = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/krpano/Google");
                           
                            SetElemValue(elem_Google, "lng", g_set.GetAttribute("lon"));
                            SetElemValue(elem_Google, "lat", g_set.GetAttribute("lat"));
                            SetElemValue(elem_Google, "zoom", "18");
                            SetElemValue(elem_Google, "heading", g_set.GetAttribute("heading").Replace("°",""));
                            SetElemValue(elem_Google, "height", g_set.GetAttribute("height"));
                            SetElemValue(elem_Google, "distance", g_set.GetAttribute("distance"));
                            #endregion

                            #endregion

                            if (action == "add")
                            {
                                try
                                {
                                    Directory.CreateDirectory(Server.MapPath(String.Format("/Project/{0}/scene/{1}", _Tour_ID, _Scene_ID)));
                                    MyApp.Components.KRPano.Scene.UploadScenePic("b", _Tour_ID, _Scene_ID, fi.FullName);
                                    MyApp.Components.XML.XmlDocument.WriteAllText(MyApp.Components.KRPano.Scene.GetSceneServerFilePath(_Tour_ID, _Scene_ID), xmldoc.OuterXml);

                                    //MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(String.Format("/Project/{0}/scene/{1}/include_scene.xml", _Tour_ID, _Scene_ID)), xmldoc.ChildNodes[0].InnerXml);
                                    i_di.Delete(true);
                                }
                                catch (Exception) { ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('Error!!!);</script>"); }
                            }
                        }
                        catch (Exception ex)
                        {
                            ButPostStreet.Enabled = false;
                            ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('"+ex.ToString()+"!!!');</script>");
                            return;
                        }
                    }
                }

                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>top.updatescenetree();top.getmainFrame().location='List_TreeScene.aspx?Tour_ID=" + _Tour_ID + "&TypeID=" + _TypeID + "';top.close_dialog();</script>");
            }
        }
    }
}