using System;
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 System.Web.UI.WebControls.WebParts;
using digioz.Themes;
using digioz.ThemeManagement;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;

public partial class controls_admin_ManageStyleSheet : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            cssPropTree.SelectedNodeChanged += new EventHandler(cssPropTree_SelectedNodeChanged);
            btnSetValues.Click += new EventHandler(btnSetValues_Click);
            btnReset.Click += new EventHandler(btnReset_Click);
            btnSave.Click += new EventHandler(btnSave_Click);
            btnRestore.Click += new EventHandler(btnRestore_Click);
            radElemType.SelectedIndexChanged += new EventHandler(radElemType_SelectedIndexChanged);
            btnStatus.Click += new EventHandler(btnStatus_Click);
        }
        else
        {
            setupHtmlElements();
            setupCSSAttributes();
            LoadBackupCSSFiles();
            btnSetValues.Enabled = false;
        }
    }

    void btnStatus_Click(object sender, EventArgs e)
    {
        pnlStatus.Visible = false;
    }

    void radElemType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (radElemType.Text.ToLower() == "html")
        {
            if (ddlHTMLElemType.Items.Count <= 0)
            {
                setupHtmlElements();
            }
            ddlHTMLElemType.Enabled = true;
            ddlOtherElemType.Enabled = false;
            ddlObjectType.Enabled = false;
        }
        else if (radElemType.Text.ToLower() == "object")
        {
            if (ddlObjectType.Items.Count <= 0)
            {
                setupSpecificElements();
            }
            ddlHTMLElemType.Enabled = false;
            ddlOtherElemType.Enabled = false;
            ddlObjectType.Enabled = true;
        }
        else
        {
            if (ddlOtherElemType.Items.Count <= 0)
            {
                setupOtherElements();
            }
            ddlHTMLElemType.Enabled = false;
            ddlOtherElemType.Enabled = true;
            ddlObjectType.Enabled = false;
        }
    }

    void btnRestore_Click(object sender, EventArgs e)
    {
        if (ddlBackupCSSFiles.SelectedValue != "")
        {
            try
            {
                File.Copy(this.MapPath("../../App_Code/").ToString() + ddlBackupCSSFiles.SelectedValue, this.MapPath("../../").ToString() + "Style.css", true);
            }
            catch (Exception leEx)
            {
                lblStatus.Text = "Error Occurred while trying to restore the selected Stylesheet. " + leEx.Message;
                pnlStatus.Visible = true;
            }
        }
    }

    void btnSave_Click(object sender, EventArgs e)
    {
        string lsNow = System.DateTime.Now.ToString().Replace("/", "").Replace(" ", "").Replace(":", "");
        string lsUser = Session["username"].ToString();
        string lsAuthorNotes;
        lsAuthorNotes = "/*" + '\r';
        lsAuthorNotes += "Saved by user `" + lsUser + "` on " + System.DateTime.Now.ToString() + '\r';
        lsAuthorNotes += "*/" + '\r';

        try
        {
            if ((CSSString.SSString != null) || (txtCSSString.Text.Trim() != ""))
            {
                CSSString.SSString = CSSString.CheckAndAddDefaultAttributes(CSSString.SSString);
                txtCSSString.Text = CSSString.SSString;
                File.Copy(this.MapPath("../../").ToString() + "Style.css", this.MapPath("../../App_Code/").ToString() + "Style_" + lsNow + "_" + lsUser + ".css", true);
                CSSString.SSString = lsAuthorNotes + CSSString.SSString;
                File.WriteAllText(this.MapPath("../../").ToString() + "Style.css", CSSString.SSString);
            }
        }
        catch (Exception leEx)
        {
            lblStatus.Text = "Error Occurred while trying to save the Stylesheet data. " + leEx.Message;
            pnlStatus.Visible = true;
        }

    }

    void btnReset_Click(object sender, EventArgs e)
    {
        CSSManager.cloCSSCurrAttributeList = new CSSCurrAttributeList();
        CSSString.SSString = "";
        txtCSSString.Text = "";
    }

    void btnSetValues_Click(object sender, EventArgs e)
    {

        bool lbPresent = false;
        CSSAttribute loCSSAttrLoc = new CSSAttribute();
        CSSAttribute loCSSAttrTmp = new CSSAttribute();
        string lsAttrValTmp = "";

        if (ddlAttrAllowedValues.Visible == true)
        {
            if (ddlAttrAllowedValues.SelectedValue != "")
            {
                lsAttrValTmp = ddlAttrAllowedValues.Text;
            }
        }
        else if (ctlColorPicker.Visible == true)
        {
            lsAttrValTmp = System.Drawing.ColorTranslator.ToHtml(ctlColorPicker.SelectedColor);
        }
        else if (txtImageFilePath.Visible == true)
        {
            if (txtImageFilePath.Text != "")
            {
                lsAttrValTmp = txtImageFilePath.Text;
            }
        }
        if (lsAttrValTmp != "")
        {
            CSSManager.cloCSSAllAttributeList[CSSString.CurrAttrPos].SelectedValue = lsAttrValTmp;
        }

        if (radElemType.Text.ToLower() == "html")
        {
            if ((CSSString.CurrAttrPos >= 0) && (lsAttrValTmp != ""))
            {
                CSSManager.cloCSSAllAttributeList[CSSString.CurrAttrPos].TargetElement = ddlHTMLElemType.Text;
                CSSManager.cloCSSAllAttributeList[CSSString.CurrAttrPos].TargetType = CSSTargetType.SimpleHtml;
            }
        }
        else if (radElemType.Text.ToLower() == "object")
        {
            if ((CSSString.CurrAttrPos >= 0) && (lsAttrValTmp != ""))
            {
                CSSManager.cloCSSAllAttributeList[CSSString.CurrAttrPos].TargetElement = ddlObjectType.Text;
                CSSManager.cloCSSAllAttributeList[CSSString.CurrAttrPos].TargetType = CSSTargetType.SpecificObject;
            }
        }
        else
        {
            if ((CSSString.CurrAttrPos >= 0) && (lsAttrValTmp != ""))
            {
                CSSManager.cloCSSAllAttributeList[CSSString.CurrAttrPos].TargetElement = ddlOtherElemType.Text;
                CSSManager.cloCSSAllAttributeList[CSSString.CurrAttrPos].TargetType = CSSTargetType.SpecificClass;
            }
        }
        if (CSSString.CurrAttrPos >= 0)
        {
            loCSSAttrLoc = CSSManager.cloCSSAllAttributeList[CSSString.CurrAttrPos];
        }

        foreach(CSSAttribute loCC in CSSManager.cloCSSCurrAttributeList)
        {
            if ((loCC.ActualName == loCSSAttrLoc.ActualName) && (loCC.AllowedValues == loCSSAttrLoc.AllowedValues)
                && (loCC.AttributeType == loCSSAttrLoc.AttributeType) && (loCC.DefaultValue == loCSSAttrLoc.DefaultValue)
                && (loCC.DisplayName == loCSSAttrLoc.DisplayName) && (loCC.TargetElement == loCSSAttrLoc.TargetElement))
            {
                lbPresent = true;
                loCC.SelectedValue = loCSSAttrLoc.SelectedValue;
                loCC.TargetType = loCSSAttrLoc.TargetType;
            }
        }

        if (!lbPresent){
            loCSSAttrTmp.ActualName = loCSSAttrLoc.ActualName;
            loCSSAttrTmp.AllowedValues = loCSSAttrLoc.AllowedValues;
            loCSSAttrTmp.AttributeType = loCSSAttrLoc.AttributeType;
            loCSSAttrTmp.DefaultValue = loCSSAttrLoc.DefaultValue;
            loCSSAttrTmp.DisplayName = loCSSAttrLoc.DisplayName;
            loCSSAttrTmp.SelectedValue = loCSSAttrLoc.SelectedValue;
            loCSSAttrTmp.TargetElement = loCSSAttrLoc.TargetElement;
            loCSSAttrTmp.TargetType = loCSSAttrLoc.TargetType;
            loCSSAttrTmp.ValueSource = loCSSAttrLoc.ValueSource;
            CSSManager.cloCSSCurrAttributeList.Add(loCSSAttrTmp);
        }

        if (CSSString.CurrAttrPos >= 0)
        {
            CSSString.CreateCompleteCSSString();
            txtCSSString.Text = "";
            txtCSSString.Text = CSSString.SSString;
        }
    }

    void cssPropTree_SelectedNodeChanged(object sender, EventArgs e)
    {
        int liAttrPos = -1;
        cssPropTree.SelectedNode.Select();
        cssPropTree.SelectedNode.ChildNodes.Clear();
        ddlAttrAllowedValues.DataSource = "";
        ddlAttrAllowedValues.DataBind();
        string strCurrNode = cssPropTree.SelectedNode.Value;

        btnSetValues.Enabled = false;
        foreach (CSSAttribute loCss in CSSManager.cloCSSAllAttributeList)
        {
            if (strCurrNode == loCss.AttributeType.ToString())
            {
                TreeNode otnTmp = new TreeNode(loCss.DisplayName);
                cssPropTree.SelectedNode.ChildNodes.Add(otnTmp);
            }
        }

        if (cssPropTree.SelectedNode.ChildNodes.Count == 0)
        {
            foreach (CSSAttribute loCss in CSSManager.cloCSSAllAttributeList)
            {
                liAttrPos++;
                if (strCurrNode == loCss.DisplayName)
                {
                    CSSString.CurrAttrPos = liAttrPos;
                    switch (loCss.ValueSource)
                    {
                        case CSSValueSource.List :
                            ddlAttrAllowedValues.DataSource = loCss.AllowedValues;
                            ddlAttrAllowedValues.DataBind();
                            ctlColorPicker.Visible = false;
                            txtImageFilePath.Visible = false;
                            ddlAttrAllowedValues.Visible = true;
                            lblAttrValsTtl.Text = "Attribute Values:";
                            btnSetValues.Enabled = true;
                            break;
                        case CSSValueSource.ColorPicker :
                            ddlAttrAllowedValues.Visible = false;
                            ctlColorPicker.Visible = true;
                            lblAttrValsTtl.Text = "Pick Color:";
                            txtImageFilePath.Visible = false;
                            btnSetValues.Enabled = true;
                            break;
                        case CSSValueSource.FilePath :
                            lblAttrValsTtl.Text = "Enter File Path:";
                            ddlAttrAllowedValues.Visible = false;
                            ctlColorPicker.Visible = false;
                            txtImageFilePath.Visible = true;
                            btnSetValues.Enabled = true;
                            break;
                    }
                }
            }
        }


    }

    public void setupHtmlElements()
    {
        CSSManager.InitHtmlElementDetails();
        ddlHTMLElemType.DataSource = CSSManager.cloHtmlElements;
        ddlHTMLElemType.DataBind();
    }

    public void setupSpecificElements()
    {
        CSSManager.InitOtherElementDetails();
        ddlObjectType.DataSource = CSSManager.cloSpecificObjects;
        ddlObjectType.DataBind();
    }

    public void setupOtherElements()
    {
        CSSManager.InitOtherElementDetails();
        ddlOtherElemType.DataSource = CSSManager.cloOtherElements;
        ddlOtherElemType.DataBind();
    }

    public void setupCSSAttributes()
    {
        CSSManager.InitAttributeDetails();

        foreach (string lsCssAttrTp in System.Enum.GetNames(typeof(CSSAttributeType)))
        {
            TreeNode ltnTmp = new TreeNode(lsCssAttrTp);
            cssPropTree.Nodes.Add(ltnTmp);
        }
    }

    public void LoadBackupCSSFiles()
    {
        DirectoryInfo ldiTmp = new DirectoryInfo(this.MapPath("../../App_Code/"));
        FileInfo[] lfiCssFiles = ldiTmp.GetFiles("*.css");
        ArrayList larlCSSBkupFiles = new ArrayList();
        for (int i = 0; i < lfiCssFiles.Length; i++)
        {
            larlCSSBkupFiles.Add(lfiCssFiles[i].Name);
        }
        ddlBackupCSSFiles.DataSource = larlCSSBkupFiles;
        ddlBackupCSSFiles.DataBind();
    }

}
