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
{
    [AjaxPro.AjaxNamespace("AjaxDemo")]
    public partial class index : MyApp.Web.UI.UserPage
    {
        public string _Tour_ID = "";

        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Tour_ID = Request.QueryString["Tour_ID"].Trim();
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
            base.AppInit();
        }

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

        }

        protected override void pl_register_script()
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(index));
            base.pl_register_script();
        }

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

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BuildTree();
            }
        }

        protected void BuildTree()
        {
            XmlDocument xmldoc = MyApp.Components.XML.XmlDocument.Load("/Data/project_config.xml");
            XmlElement elem_Project = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, "/config/tree");

            TV_Tree.Nodes.Clear();
            ComponentArt.Web.UI.TreeViewNode Project_Node = CreateNode(elem_Project.GetAttribute("Text").Replace("{text}", GetValue("/Project", "Name")), elem_Project.GetAttribute("Value"), Convert.ToBoolean(elem_Project.GetAttribute("Expanded")), elem_Project.GetAttribute("ImageUrl"), elem_Project.GetAttribute("NavigateUrl").Replace("{Tour_ID}", _Tour_ID), elem_Project.GetAttribute("Target"), false);
            TV_Tree.Nodes.Add(Project_Node);

            XmlNodeList xmlnodelist = xmldoc.SelectNodes("/config/tree/node");
            foreach (XmlElement elem_item in xmlnodelist)
            {
                ComponentArt.Web.UI.TreeViewNode new_Node = CreateNode(elem_item.GetAttribute("Text"), elem_item.GetAttribute("Value"), Convert.ToBoolean(elem_item.GetAttribute("Expanded")), elem_item.GetAttribute("ImageUrl"), elem_item.GetAttribute("NavigateUrl").Replace("{Tour_ID}", _Tour_ID), elem_item.GetAttribute("Target"), false);
                Project_Node.Nodes.Add(new_Node);
            }
        }

        [AjaxPro.AjaxMethod]
        public DataRow[] Get_Scenes(string _Tour_ID)
        {
            return MyApp.Components.KRPano.Tour.Get_Scenes(_Tour_ID);
        }

        [AjaxPro.AjaxMethod]
        public DataRow[] Get_TypeScenes(string _Tour_ID, string _Type_ID)
        {
            return MyApp.Components.KRPano.Tour.Get_Scenes(_Tour_ID, _Type_ID);
        }

        [AjaxPro.AjaxMethod]
        public MyApp.Components.KRPano.tournode[] Get_KRPanoNode(string _Tour_ID, string _XPath)
        {
            return MyApp.Components.KRPano.Tour.Get_KRPanoNode(_Tour_ID, _XPath);
        }

        [AjaxPro.AjaxMethod]
        public string Get_KRPano_NodeValue(string _Tour_ID, int _ROW, string _Attribute, string _XPath)
        {
            XmlDocument xmldoc = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID));
            XmlElement elem_plugin = (XmlElement)xmldoc.SelectNodes(_XPath)[_ROW - 1];

            return elem_plugin.GetAttribute(_Attribute);
        }

        [AjaxPro.AjaxMethod]
        public MyApp.Components.KRPano.tournode[] Get_Plugins(string _Tour_ID)
        {
            return MyApp.Components.KRPano.Tour.Get_Plugins(_Tour_ID);
        }
        [AjaxPro.AjaxMethod]
        public string Get_Plugin_NodeValue(string _Tour_ID, int _ROW, string _Attribute)
        {
            XmlDocument xmldoc = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID));
            XmlElement elem_plugin = (XmlElement)xmldoc.SelectNodes("/Project/Plugins/plugin")[_ROW - 1];

            return elem_plugin.GetAttribute(_Attribute);
        }

        [AjaxPro.AjaxMethod]
        public MyApp.Components.KRPano.tournode[] Get_CustomMaps(string _Tour_ID)
        {
            return MyApp.Components.KRPano.Tour.Get_CustomMaps(_Tour_ID);
        }

        [AjaxPro.AjaxMethod]
        public MyApp.Components.KRPano.scenetypenode[] Get_TypeTree(string _Tour_ID)
        {
            return MyApp.Components.KRPano.Tour.Get_TypeTree(_Tour_ID);
        }

        [AjaxPro.AjaxMethod]
        public MyApp.Components.KRPano.pcollecttypenode[] Get_PCollectType(string _Tour_ID)
        {
            return MyApp.Components.KRPano.Tour.Get_PCollectType(_Tour_ID);
        }

        [AjaxPro.AjaxMethod]
        public string Get_CustomMap_NodeValue(string _Tour_ID, int _ROW, string _Attribute)
        {
            XmlDocument xmldoc = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID));
            XmlElement elem_plugin = (XmlElement)xmldoc.SelectNodes("/Project/Maps/Map")[_ROW - 1];

            return elem_plugin.GetAttribute(_Attribute);
        }

        [AjaxPro.AjaxMethod]
        public string GetElemValueByPath(object _Tour_ID, object scene_id, string attribute)
        {
            string _File_Dir = Server.MapPath(String.Format("/Project/{0}/Scene/{1}/scene.xml", _Tour_ID, scene_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 "";
            }
        }

        public string GetElemValueByPath(object scene_id, string attribute)
        {
            return GetElemValueByPath(_Tour_ID, scene_id, attribute);
        }
        private ComponentArt.Web.UI.TreeViewNode CreateNode(string name, string id, bool expanded, string imageurl, string navurl, string target, bool _ShowCheckBox)
        {
            ComponentArt.Web.UI.TreeViewNode node = new ComponentArt.Web.UI.TreeViewNode();
            node.Text = name;
            node.NavigateUrl = navurl;
            node.Target = target;
            node.Value = id.ToString();
            node.ImageUrl = imageurl;
            node.Expanded = expanded;
            node.ShowCheckBox = _ShowCheckBox;
            return node;
        }

        [AjaxPro.AjaxMethod]
        public string GetValue(string _Tour_ID, string path, string attribute)
        {
            return MyApp.Components.KRPano.Project.Get_ProjectNodeConfigValue(_Tour_ID, path, attribute);
        }

        public string GetValue(string path, string attribute)
        {
            return GetValue(_Tour_ID, path, attribute);
        }

        [AjaxPro.AjaxMethod]
        public string GetNodeConfigValue(string path, string attribute)
        {
            XmlDocument xmldoc = MyApp.Components.XML.XmlDocument.Load("/Data/project_config.xml");
            XmlElement xmlelem = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, path);
            if (xmlelem.GetAttribute(attribute).Trim() != "")
            {
                return xmlelem.GetAttribute(attribute).Trim();
            }
            else
            {
                return " ";
            }
        }
    }
}