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 AD_Plugin : MyApp.Web.UI.UserPage
    {
        public string _Scene_ID = "", _Tour_ID = "";
        public int _ROW = 0;

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

        override protected void DataInit()//数据初始化
        {
            XmlDocument xmldoc = MyApp.Components.XML.XmlDocument.Load(MyApp.Components.KRPano.Project.Get_Project_Name(_Tour_ID));
            XmlNodeList xmlnodelist_plugin = xmldoc.SelectNodes("/Project/Plugins/plugin");
            foreach (XmlElement xmlele_plugin in xmlnodelist_plugin)
            {
                this.parent.Items.Add(new ListItem("漫游插件: " + xmlele_plugin.GetAttribute("name"), xmlele_plugin.GetAttribute("name")));
                this.mask.Items.Add(new ListItem("漫游插件: " + xmlele_plugin.GetAttribute("name"), xmlele_plugin.GetAttribute("name")));
            }

            XmlDocument scene_xmldoc = MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, _Scene_ID);
            XmlNodeList xmlnodelist_splugin = scene_xmldoc.SelectNodes("/krpano/plugin");

            foreach (XmlElement xmlele_plugin in xmlnodelist_splugin)
            {
                this.parent.Items.Add(new ListItem("场景插件: " + xmlele_plugin.GetAttribute("name"), xmlele_plugin.GetAttribute("name")));
                this.mask.Items.Add(new ListItem("场景插件: " + xmlele_plugin.GetAttribute("name"), xmlele_plugin.GetAttribute("name")));
            }

            XmlNodeList xmlnodelist_hotspot = scene_xmldoc.SelectNodes("/krpano/hotspot");
            foreach (XmlElement xmlele_plugin in xmlnodelist_hotspot)
            {
                this.parent.Items.Add(new ListItem("场景热点: " + xmlele_plugin.GetAttribute("name"),"hotspot["+ xmlele_plugin.GetAttribute("name")+"]"));
            }

            XmlNodeList xmlnodelist_layer = scene_xmldoc.SelectNodes("/krpano/layer");
            foreach (XmlElement xmlele_plugin in xmlnodelist_layer)
            {
                this.parent.Items.Add(new ListItem("场景layer: " + xmlele_plugin.GetAttribute("name"), "layer[" + xmlele_plugin.GetAttribute("name") + "]"));
            }
        }

        protected override void pl_register_script()
        {
            this.T_HtmlPluginTF.Attributes.Add("onclick", "javascrpt:if ($('T_HtmlPluginTF').checked){$('url').value='%SWFPATH%/plugins/textfield.swf';}");
            base.pl_register_script();
        }
      
        override protected void pl_del()
        {
            XmlDocument xmldoc;
            xmldoc = MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, _Scene_ID);
            XmlNode xmlnode = xmldoc.SelectSingleNode("/krpano");
            XmlNodeList xmlnodelist = xmldoc.SelectNodes("/krpano/plugin");
            try
            {
                if (_ROW == -1)
                {
                    foreach (XmlElement elem_plugin in xmlnodelist)
                    {
                        xmlnode.RemoveChild(elem_plugin);
                    }
                }
                else
                {
                    if (xmlnodelist.Count >= _ROW)
                    {
                        xmlnode.RemoveChild(xmlnodelist[_ROW - 1]);
                    }
                }
                MyApp.Components.XML.XmlDocument.WriteAllText(MyApp.Components.KRPano.Scene.GetSceneServerFilePath(_Tour_ID, _Scene_ID), xmldoc.OuterXml);
                Response.Redirect(MyApp.Components.NavUrl.GetUrl(3));
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
            }

        override protected void pl_add()
        {
            this.name.Focus();
            if (_ROW > 0)
            {
                pl_edit();
            }
        }
        override protected void pl_edit()
        {
            XmlDocument xmldoc;
            xmldoc = MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, _Scene_ID);
            try
            {
                XmlElement elem_plugin = (XmlElement)xmldoc.SelectNodes("/krpano/plugin")[_ROW - 1];

                GetElemValue(elem_plugin, "name", this.name, true);
                GetElemValue(elem_plugin, "url", this.url, true);
                GetElemValue(elem_plugin, "zorder", this.zorder, true);
                GetElemValue(elem_plugin, "alpha", this.alpha, true);
                GetElemValue(elem_plugin, "blendmode", this.blendmode, true);
                GetElemValue(elem_plugin, "align", this.align, true);
                GetElemValue(elem_plugin, "edge", this.edge, true);
                GetElemValue(elem_plugin, "x", this.x, true);
                GetElemValue(elem_plugin, "y", this.y, true);

                GetElemValue(elem_plugin, "accuracy", this.accuracy, true);
                GetElemValue(elem_plugin, "usecontentsize", this.usecontentsize, true);

                GetElemValue(elem_plugin, "ox", this.ox, true);
                GetElemValue(elem_plugin, "oy", this.oy, true);

                GetElemValue(elem_plugin, "width", this.width, true);
                GetElemValue(elem_plugin, "height", this.height, true);
                GetElemValue(elem_plugin, "refreshrate", this.refreshrate, true);
                GetElemValue(elem_plugin, "scale", this.scale, true);
                GetElemValue(elem_plugin, "smoothing", this.smoothing, true);

                GetElemValue(elem_plugin, "mask", this.mask, true);
                GetElemValue(elem_plugin, "rotate", this.rotate, true);

                GetElemValue(elem_plugin, "visible", this.visible, true);
                GetElemValue(elem_plugin, "enabled", this.enabled, true);
                GetElemValue(elem_plugin, "capture", this.capture, true);
                GetElemValue(elem_plugin, "handcursor", this.handcursor, true);
                GetElemValue(elem_plugin, "keep", this.keep, true);
                GetElemValue(elem_plugin, "children", this.children, true);
                GetElemValue(elem_plugin, "preload", this.preload, true);
                GetElemValue(elem_plugin, "Intro", this.Intro, true);

                GetElemValue(elem_plugin, "style", this.style, true);

                #region 效果
                GetElemValue(elem_plugin, "effect", this.effect, true);
                #endregion

                #region 父子控制
                GetElemValue(elem_plugin, "parent", this.parent, true);
                GetElemValue(elem_plugin, "scalechildren", this.scalechildren, true);
                #endregion

                #region Crop/Scale9Grid
                GetElemValue(elem_plugin, "crop", this.crop, true);
                GetElemValue(elem_plugin, "onovercrop", this.onovercrop, true);
                GetElemValue(elem_plugin, "ondowncrop", this.ondowncrop, true);

                GetElemValue(elem_plugin, "scale9grid", this.scale9grid, true);
                #endregion

                #region HTML5/APPLE
                GetElemValue(elem_plugin, "devices", this.devices, true);
                GetElemValue(elem_plugin, "alturl", this.alturl, true);
                GetElemValue(elem_plugin, "altscale", this.altscale, true);
                GetElemValue(elem_plugin, "jsborder", this.jsborder, true);
                GetElemValue(elem_plugin, "altonloaded", this.altonloaded, true);
                #endregion

                #region 雨点效果
                GetElemValue(elem_plugin, "type", this.type, true);
                GetElemValue(elem_plugin, "imageurl", this.imageurl, true);
                GetElemValue(elem_plugin, "color", this.color, true);
                GetElemValue(elem_plugin, "flakes", this.flakes, true);
                GetElemValue(elem_plugin, "imagescale", this.imagescale, true);
                GetElemValue(elem_plugin, "speed", this.speed, true);
                GetElemValue(elem_plugin, "shake", this.shake, true);
                GetElemValue(elem_plugin, "speedvariance", this.speedvariance, true);
                GetElemValue(elem_plugin, "spreading", this.spreading, true);
                GetElemValue(elem_plugin, "rainwidth", this.rainwidth, true);
                GetElemValue(elem_plugin, "rainalpha", this.rainalpha, true);
                #endregion

                #region 事件
                GetElemValue(elem_plugin, "onover", this.onover, true);
                GetElemValue(elem_plugin, "onhover", this.onhover, true);
                GetElemValue(elem_plugin, "onout", this.onout, true);
                GetElemValue(elem_plugin, "onclick", this.onclick, true);
                GetElemValue(elem_plugin, "ondown", this.ondown, true);
                GetElemValue(elem_plugin, "onup", this.onup, true);
                GetElemValue(elem_plugin, "onloaded", this.onloaded, true);
                #endregion

                #region FlvVideo
                GetElemValue(elem_plugin, "posterurl", this.posterurl, true);
                GetElemValue(elem_plugin, "videourl", this.videourl, true);
                GetElemValue(elem_plugin, "altvideourl", this.altvideourl, true);
                GetElemValue(elem_plugin, "volume", this.volume, true);
                GetElemValue(elem_plugin, "range", this.range, true);
                GetElemValue(elem_plugin, "directionalsound", this.directionalsound, true);
                GetElemValue(elem_plugin, "buffertime", this.buffertime, true);
                GetElemValue(elem_plugin, "loop", this.loop, true);
                GetElemValue(elem_plugin, "pausedonstart", this.pausedonstart, true);
                GetElemValue(elem_plugin, "updateeveryframe", this.updateeveryframe, true);
                GetElemValue(elem_plugin, "onvideocomplete", this.updateeveryframe, true);
                #endregion

                #region 内容插件
                GetElemValue(elem_plugin, "html", this.html, true);
                GetElemValue(elem_plugin, "css", this.css, true);
                GetElemValue(elem_plugin, "autosize", this.autosize, true);
                GetElemValue(elem_plugin, "background", this.background, true);
                GetElemValue(elem_plugin, "backgroundcolor", this.backgroundcolor, true);
                GetElemValue(elem_plugin, "bordercolor", this.bordercolor, true);
                GetElemValue(elem_plugin, "borderwidth", this.borderwidth, true);
                GetElemValue(elem_plugin, "roundedge", this.roundedge, true);
                GetElemValue(elem_plugin, "selectable", this.selectable, true);
                GetElemValue(elem_plugin, "glow", this.glow, true);
                GetElemValue(elem_plugin, "glowcolor", this.glowcolor, true);
                GetElemValue(elem_plugin, "blur", this.blur, true);
                GetElemValue(elem_plugin, "shadow", this.shadow, true);
                GetElemValue(elem_plugin, "textglow", this.textglow, true);
                GetElemValue(elem_plugin, "textblur", this.textblur, true);
                GetElemValue(elem_plugin, "textglowcolor", this.textglowcolor, true);
                GetElemValue(elem_plugin, "textshadow", this.textshadow, true);
                #endregion
            }
            catch (Exception)
            {
                ButPost.Enabled = false;
                ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('错误!!!');history.back()</script>");
                return;
            }
        }

        protected void ButPost_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlElement elem_plugin;
            if (Page.IsValid)
            {
                try
                {
                    if (action == "add")
                    {
                        xmldoc = MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, _Scene_ID);
                        elem_plugin = xmldoc.CreateElement("plugin");
                    }
                    else
                        if (action == "edit")
                        {
                            xmldoc = MyApp.Components.KRPano.Scene.GetSceneXmlDoc(_Tour_ID, _Scene_ID);
                            elem_plugin = (XmlElement)xmldoc.SelectNodes("/krpano/plugin")[_ROW - 1];
                        }
                        else
                        {
                            return;
                        }
                    #region 赋值区域

                    SetElemValue(elem_plugin, "name", this.name);
                    SetElemValue(elem_plugin, "url", this.url);
                    SetElemValue(elem_plugin, "zorder", this.zorder);
                    SetElemValue(elem_plugin, "alpha", this.alpha);
                    SetElemValue(elem_plugin, "blendmode", this.blendmode);
                    SetElemValue(elem_plugin, "align", this.align);
                    SetElemValue(elem_plugin, "edge", this.edge);
                    SetElemValue(elem_plugin, "x", this.x);
                    SetElemValue(elem_plugin, "y", this.y);

                    SetElemValue(elem_plugin, "accuracy", this.accuracy);
                    SetElemValue(elem_plugin, "usecontentsize", this.usecontentsize);

                    SetElemValue(elem_plugin, "ox", this.ox);
                    SetElemValue(elem_plugin, "oy", this.oy);

                    SetElemValue(elem_plugin, "width", this.width);
                    SetElemValue(elem_plugin, "height", this.height);
                    SetElemValue(elem_plugin, "refreshrate", this.refreshrate);
                    SetElemValue(elem_plugin, "scale", this.scale);
                    SetElemValue(elem_plugin, "smoothing", this.smoothing);

                    SetElemValue(elem_plugin, "mask", this.mask);
                    SetElemValue(elem_plugin, "rotate", this.rotate);

                    SetElemValue(elem_plugin, "visible", this.visible);
                    SetElemValue(elem_plugin, "enabled", this.enabled);
                    SetElemValue(elem_plugin, "capture", this.capture);
                    SetElemValue(elem_plugin, "handcursor", this.handcursor);
                    SetElemValue(elem_plugin, "keep", this.keep);
                    SetElemValue(elem_plugin, "children", this.children);
                    SetElemValue(elem_plugin, "preload", this.preload);
                    SetElemValue(elem_plugin, "Intro", this.Intro);

                    SetElemValue(elem_plugin, "style", this.style);

                    #region 效果
                    SetElemValue(elem_plugin, "effect", this.effect);
                    #endregion

                    #region 父子控制
                    SetElemValue(elem_plugin, "parent", this.parent);
                    SetElemValue(elem_plugin, "scalechildren", this.scalechildren);
                    #endregion


                    #region Crop/Scale9Grid
                    SetElemValue(elem_plugin, "crop", this.crop);
                    SetElemValue(elem_plugin, "onovercrop", this.onovercrop);
                    SetElemValue(elem_plugin, "ondowncrop", this.ondowncrop);

                    SetElemValue(elem_plugin, "scale9grid", this.scale9grid);
                    #endregion

                    #region HTML5/APPLE
                    SetElemValue(elem_plugin, "devices", this.devices);
                    if (this.alturl.Text.Trim() == "")
                    {
                        //this.alturl.Text = this.url.Text;
                    }
                    SetElemValue(elem_plugin, "alturl", this.alturl);
                    SetElemValue(elem_plugin, "altscale", this.altscale);
                    SetElemValue(elem_plugin, "jsborder", this.jsborder);
                    SetElemValue(elem_plugin, "altonloaded", this.altonloaded);
                    #endregion



                    #region 雨点效果
                    SetElemValue(elem_plugin, "type", this.type);
                    SetElemValue(elem_plugin, "imageurl", this.imageurl);
                    SetElemValue(elem_plugin, "color", this.color);
                    SetElemValue(elem_plugin, "flakes", this.flakes);
                    SetElemValue(elem_plugin, "imagescale", this.imagescale);
                    SetElemValue(elem_plugin, "speed", this.speed);
                    SetElemValue(elem_plugin, "shake", this.shake);
                    SetElemValue(elem_plugin, "speedvariance", this.speedvariance);
                    SetElemValue(elem_plugin, "spreading", this.spreading);
                    SetElemValue(elem_plugin, "rainwidth", this.rainwidth);
                    SetElemValue(elem_plugin, "rainalpha", this.rainalpha);
                    #endregion

                    #region 事件
                    SetElemValue(elem_plugin, "onover", this.onover);
                    SetElemValue(elem_plugin, "onhover", this.onhover);
                    SetElemValue(elem_plugin, "onout", this.onout);
                    SetElemValue(elem_plugin, "onclick", this.onclick);
                    SetElemValue(elem_plugin, "ondown", this.ondown);
                    SetElemValue(elem_plugin, "onup", this.onup);
                    SetElemValue(elem_plugin, "onloaded", this.onloaded);
                    #endregion

                    #region 内容插件
                    this.html.Text = this.html.Text.Replace("<strong>", "<b>").Replace("</strong>", "</b>");
                    SetElemValue(elem_plugin, "html", this.html);
                    SetElemValue(elem_plugin, "css", this.css);
                    SetElemValue(elem_plugin, "autosize", this.autosize);
                    SetElemValue(elem_plugin, "background", this.background);
                    SetElemValue(elem_plugin, "backgroundcolor", this.backgroundcolor);
                    SetElemValue(elem_plugin, "bordercolor", this.bordercolor);
                    SetElemValue(elem_plugin, "borderwidth", this.borderwidth);
                    SetElemValue(elem_plugin, "roundedge", this.roundedge);
                    SetElemValue(elem_plugin, "selectable", this.selectable);
                    SetElemValue(elem_plugin, "glow", this.glow);
                    SetElemValue(elem_plugin, "glowcolor", this.glowcolor);
                    SetElemValue(elem_plugin, "blur", this.blur);
                    SetElemValue(elem_plugin, "shadow", this.shadow);
                    SetElemValue(elem_plugin, "textglow", this.textglow);
                    SetElemValue(elem_plugin, "textblur", this.textblur);
                    SetElemValue(elem_plugin, "textglowcolor", this.textglowcolor);
                    SetElemValue(elem_plugin, "textshadow", this.textshadow);
                    #endregion

                    #region FlvVideo
                    SetElemValue(elem_plugin, "posterurl", this.posterurl, true);
                    SetElemValue(elem_plugin, "videourl", this.videourl, true);
                    SetElemValue(elem_plugin, "altvideourl", this.altvideourl, true);
                    SetElemValue(elem_plugin, "volume", this.volume);
                    SetElemValue(elem_plugin, "range", this.range);
                    SetElemValue(elem_plugin, "directionalsound", this.directionalsound);
                    SetElemValue(elem_plugin, "buffertime", this.buffertime);
                    SetElemValue(elem_plugin, "loop", this.loop);
                    SetElemValue(elem_plugin, "pausedonstart", this.pausedonstart);
                    SetElemValue(elem_plugin, "updateeveryframe", this.updateeveryframe);
                    SetElemValue(elem_plugin, "onvideocomplete", this.updateeveryframe);
                    #endregion

                    #endregion
                    if (action == "add")
                    {
                        try
                        {
                            xmldoc.SelectSingleNode("/krpano").AppendChild(elem_plugin);
                            MyApp.Components.XML.XmlDocument.WriteAllText(MyApp.Components.KRPano.Scene.GetSceneServerFilePath(_Tour_ID, _Scene_ID), xmldoc.OuterXml);
                            Response.Redirect(MyApp.Components.NavUrl.GetUrl(3));
                        }
                        catch (Exception) { ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('Error!!!);history.back()</script>"); }
                    }
                    if (action == "edit")
                    {
                        try
                        {
                            MyApp.Components.XML.XmlDocument.WriteAllText(MyApp.Components.KRPano.Scene.GetSceneServerFilePath(_Tour_ID, _Scene_ID), xmldoc.OuterXml);
                            Response.Redirect(MyApp.Components.NavUrl.GetUrl(3));
                        }
                        catch (Exception) { ClientScript.RegisterStartupScript(this.GetType(), "refresh", "<script language=javascript>alert('Error!!!);history.back()</script>"); }
                    }


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