using System;
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;

namespace MYAPP.Manage.Project.Design.Object
{
    [AjaxPro.AjaxNamespace("AjaxDemo")]
    public partial class List_Object : MyApp.Web.UI.UserPage
    {
        public int PageRecord = 50, NowPage, RecordCount, hRecordCount, MaxPage;
        public DataTable list_dt;
        public string _Tour_ID = "";
        //public string _

        protected override void AppInit()
        {
            try
            {
                _Tour_ID = Request.QueryString["Tour_ID"].Trim();
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('Error!!!');history.back()</script>");
                return;
            }
            base.AppInit();
        }

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

        protected void Page_Load(object sender, EventArgs e)
        {
            MyApp.Components.NavUrl.SetUrl(2);
            try
            {
                Page.DataBind();
            }
            catch (Exception) { }

        }

        protected override void DataInit()
        {
            list_dt = MyApp.Components.KRPano.Project.GetObjectDirectorys(String.Format("{0}/Object", _Tour_ID), "", PageRecord, ref NowPage, ref RecordCount, ref hRecordCount, ref MaxPage);
            base.DataInit();
        }

        public string GetElemValueByPath(object _Scene_ID, string attribute)
        {
            return MyApp.Components.KRPano.Scene.GetValue(MyApp.Components.KRPano.Object.GetSceneXmlDoc(_Tour_ID, _Scene_ID), "/Project", attribute);
        }

        public string GetElemValueByPath(object _Scene_ID, string path, string attribute)
        {
            return MyApp.Components.KRPano.Scene.GetValue(MyApp.Components.KRPano.Object.GetSceneXmlDoc(_Tour_ID, _Scene_ID), path, attribute);
        }

        [AjaxPro.AjaxMethod]
        public string ChangeValue(object _Tour_ID, string _Object_ID, string _Path, string _Field, object _Value)
        {
            try
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc = MyApp.Components.KRPano.Object.GetSceneXmlDoc(_Tour_ID, _Object_ID);

                #region KRPano
                XmlElement elem = (XmlElement)MyApp.Components.XML.XmlDocument.SelectSingleNode(ref xmldoc, _Path);
                SetElemValue(elem, _Field, _Value.ToString());
                #endregion

                MyApp.Components.XML.XmlDocument.WriteAllText(MyApp.Components.KRPano.Object.GetObjectServerFilePath(_Tour_ID, _Object_ID), xmldoc.OuterXml);
            }
            catch (Exception ex) { return ex.ToString(); }
            return "true";
        }


        [AjaxPro.AjaxMethod]
        public string ExportSingleSwf(object _Tour_ID, object _Object_ID)  //导出360物体SWF文件
        {
            if (System.IO.Directory.Exists(MyApp.Components.KRPano.Object.GetObjectServerDir(_Tour_ID, _Object_ID)))
            {

                COE.Components.MyFun.ChangeNameExport();

                Process process = new Process();
                try
                {
                    process.StartInfo.FileName = MyApp.Components.File.FileManage.GetRootDir("\\pano\\object.bat");
                    process.StartInfo.Arguments = string.Format("{0}\\*.jpg", MyApp.Components.KRPano.Object.GetObjectServerDir(_Tour_ID, _Object_ID));
                    process.StartInfo.WorkingDirectory = MyApp.Components.KRPano.Object.GetObjectServerDir(_Tour_ID, _Object_ID);
                    process.Start();

                    process.WaitForExit();

                    COE.Components.MyFun.ReturnNameExport();

                    try
                    {
                        File.Delete(MyApp.Components.KRPano.Object.GetObjectBaseServerDir(_Tour_ID) + "//" + _Object_ID + ".swf");
                    }
                    catch
                    {
                    }
                    try
                    {
                        File.Move(MyApp.Components.KRPano.Object.GetObjectServerDir(_Tour_ID, _Object_ID) + "/object.swf", MyApp.Components.KRPano.Object.GetObjectBaseServerDir(_Tour_ID) + "//" + _Object_ID + ".swf");
                    }
                    catch
                    {

                    }

                    DirectoryInfo di = new DirectoryInfo(MyApp.Components.KRPano.Object.GetObjectServerDir(_Tour_ID, _Object_ID));
                    FileInfo[] fis = di.GetFiles("*.jpg");

                    if (fis.Length > 0)
                    {
                        //Response.Write(fis[0].Name);
                        //return fis[0].Name;
                    }
                    // Directory.Delete(MyApp.Components.KRPano.Object.GetObjectServerDir(_Tour_ID, _Object_ID) + "/object", true);
                    return "true";
                }
                catch (Exception ex)
                {
                    COE.Components.MyFun.ReturnNameExport();
                    return ex.ToString();
                }
            }
            else
            {
                return "无法导出单个SWF场景文件!!!";
            }
        }
    }
}