using System;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Xml;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using COE.Components;
using COE.Data;
using MyApp.DBTable.tour;


namespace MYAPP.Manage.Project.Design
{
    public partial class Version_Upgrade : MyApp.Web.UI.PublicPage
    {
        public string _Tour_ID = "", _Map_ID = null;
        public string _Version = "";

        override protected void AppInit()//程序基本信息初始化
        {
            try
            {
                _Tour_ID = Request["Tour_ID"];
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
            try
            {
                _Version = Request["_Version"].Trim();
            }
            catch
            {
                _Version = "";
            }
            if (_Version == "3.3")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('项目已经是最高版!!!');history.back()</script>");
                return;
            }

            //ExportXml(base.action, _Tour_ID);
            if (VersionUpgrade(_Tour_ID) == 1)
            {
                Response.Write("<script language=javascript>alert('Successful!!!');this.location='" + MyApp.Components.NavUrl.GetUrl(1) + "'</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('Error!!!!');history.back()</script>");
            }

        }


        protected int VersionUpgrade(string _tour_id)
        {
            bool bl = false;
            int x = 0;

            //XmlDocument xmldoc = null;

            DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(MyApp.Components.KRPano.Project.Get_Pprject_Dir(_tour_id) + "/scene"));

            try
            {
                string _xmlpath = "";
                if (di.Exists)
                {
                    DirectoryInfo[] dis = di.GetDirectories();

                    foreach (DirectoryInfo dy_info in dis)
                    {
                        foreach (FileInfo file in dy_info.GetFiles("*.xml"))
                        {

                            if (file.Name == "scene.xml")
                            {
                                if(!File.Exists(file.FullName+".bak"))

                                    file.CopyTo(file.FullName + ".bak", false);


                                _xmlpath = "/Project/" + _tour_id + "/scene/" + dy_info.Name + "/" + file.Name;


                                XmlDocument xmldoc = MyApp.Components.XML.XmlDocument.Load(_xmlpath);


                                XmlElement xle_events = (XmlElement)xmldoc.SelectSingleNode("/krpano/events");


                                if (xle_events != null)
                                {
                                    for (int i = 0; i < xle_events.Attributes.Count; i++)
                                    {
                                        xle_events.Attributes[i].Value = xle_events.Attributes[i].Value.Replace("SSet.", "Set.").Replace("Set.", "SSet.").Replace("openurl(", "url(").Replace("url(", "openurl(");
                                    }

                                    if (xle_events.GetAttribute("onclick").Trim() == "")
                                    {
                                        xle_events.SetAttribute("onclick", "_onclick();");
                                    }
                                    if (xle_events.GetAttribute("onmousedown").Trim() == "")
                                    {
                                        xle_events.SetAttribute("onmousedown", "_onmousedown();");
                                    }
                                    if (xle_events.GetAttribute("onmouseup").Trim() == "")
                                    {
                                        xle_events.SetAttribute("onmouseup", "//_onmouseup();");
                                    }
                                    if (xle_events.GetAttribute("onkeydown").Trim() == "")
                                    {
                                        xle_events.SetAttribute("onkeydown", "//_onkeydown();");
                                    }
                                    if (xle_events.GetAttribute("onkeyup").Trim() == "")
                                    {
                                        xle_events.SetAttribute("onkeyup", "//_onkeyup();");
                                    }
                                }

                                XmlElement xle_set = (XmlElement)xmldoc.SelectSingleNode("/krpano/Set");

                                XmlElement xle_sset = (XmlElement)xmldoc.SelectSingleNode("/krpano/SSet");

                                if (xle_sset == null)
                                {
                                    xle_sset = xmldoc.CreateElement("SSet");
                                    if (xle_set != null)
                                    {
                                        foreach (XmlAttribute xa in xle_set.Attributes)
                                        {
                                            xle_sset.SetAttribute(xa.Name, xa.Value);
                                        }
                                        xle_set.ParentNode.AppendChild(xle_sset);
                                    }
                                }
                                if (xle_set != null)
                                {
                                    xle_set.ParentNode.RemoveChild(xle_set);
                                }
                                MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(_xmlpath), xmldoc.OuterXml.Replace("load(%FIRSTXML%scene/", "loadpano(%FIRSTXML%scene/").Replace("load(%FIRSTXML%scene/%1/scene.xml,", "loadpano(%FIRSTXML%scene/%1/scene.xml,").Replace("movevectorz", "fov_moveforce").Replace("movevectorx", "hlookat_moveforce").Replace("movevectory", "vlookat_moveforce").Replace("autoalpha=\"True\"", "autoalpha=\"False\""));
                            }
                            else if (file.Name == "include_scene.xml")
                            {
                                file.MoveTo(file.FullName + ".bak");
                            }
                        }
                    }

                    if (!File.Exists(Server.MapPath(MyApp.Components.KRPano.Project.Get_Project_Name(_tour_id) + ".bak")))
                    {
                        File.Copy(Server.MapPath(MyApp.Components.KRPano.Project.Get_Project_Name(_tour_id)), Server.MapPath(MyApp.Components.KRPano.Project.Get_Project_Name(_tour_id) + ".bak"), false);
                    }

                    XmlDocument xmldoc_pro = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(_tour_id));
                    XmlElement xmle_pro = (XmlElement)xmldoc_pro.SelectSingleNode("/Project");
                    if (xmle_pro != null)
                    {
                        SetElemValue(xmle_pro, "Version", "3.3");
                    }
                    string xmlstr = xmldoc_pro.OuterXml;
                    xmlstr = xmlstr.Replace("SSet.", "Set.").Replace("Set.", "SSet.").Replace("load(%FIRSTXML%scene/", "loadpano(%FIRSTXML%scene/").Replace("load(%FIRSTXML%scene/%1/scene.xml,", "loadpano(%FIRSTXML%scene/%1/scene.xml,").Replace("movevectorz", "fov_moveforce").Replace("movevectorx", "hlookat_moveforce").Replace("movevectory", "vlookat_moveforce").Replace("autoalpha=\"True\"", "autoalpha=\"False\"").Replace("openurl(", "url(").Replace("url(", "openurl(");
                    
                    // xmldoc_pro.LoadXml(xmlstr);
                    MyApp.Components.XML.XmlDocument.WriteAllText(Server.MapPath(MyApp.Components.KRPano.Project.Get_Project_Name(_tour_id)), xmlstr);
                    try
                    {
                        File.Copy(Server.MapPath(String.Format("/pano/COPY/Accessories/bs.png")), Server.MapPath(String.Format("/Project/{0}/Accessories/bs.png", _tour_id)), false);
                    }
                    catch
                    { }
                    try
                    {
                        File.Delete(Server.MapPath(String.Format("/Project/{0}/krpanoiphone.js", _tour_id)));
                        File.Delete(Server.MapPath(String.Format("/Project/{0}/swfkrpano.js", _tour_id)));
                        File.Copy(Server.MapPath(String.Format("/pano/COPY/krpanoiphone.js")), Server.MapPath(String.Format("/Project/{0}/krpanoiphone.js", _tour_id)), true);
                        File.Copy(Server.MapPath(String.Format("/pano/COPY/swfkrpano.js")), Server.MapPath(String.Format("/Project/{0}/swfkrpano.js", _tour_id)), true);
                    }
                    catch
                    {
                        x = -1;
                        return x;
                    }

                    try
                    {
                        File.Copy(Server.MapPath(String.Format("/Project/{0}/index.html", _tour_id)), Server.MapPath(String.Format("/Project/{0}/index.html.bak", _tour_id)), false);

                        File.Copy(Server.MapPath(String.Format("/pano/COPY/index.html")), Server.MapPath(String.Format("/Project/{0}/index.html", _tour_id)), true);

                    }
                    catch
                    { }

                    x = 1;
                }
                else
                {
                    x = 2;
                    Response.Write("文件夹不存在或者文件不完整!!");
                }
            }
            catch (Exception ex)
            {
                x = 0;
            }

            return x;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}