using System;
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;
using System.IO;
using Manoli.Utils.CSharpFormat;

public partial class ShowCode : System.Web.UI.UserControl
{
    public string CodeFile
    {
        get
        {
            return _CodeFile;
        }
        set
        {
            _CodeFile = value;
        }
    }
    public string _CodeFile = "";

    public DisplayStates DisplayState
    {
        get
        {
            return _DisplayState;
        }
        set
        {
            _DisplayState = value;
        }
    }
    public DisplayStates _DisplayState = DisplayStates.Button;

    public string Text
    {
        get
        {
            return _Text;
        }
        set
        {
            _Text = value;
            this.btnShowCode.Text = value;
        }
    }
    public string _Text = "Show Code";
    
    public Unit Width
    {
        get { return _Width; }
        set { _Width = value; }
    }
    private Unit _Width = Unit.Empty;


    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnShowCode.Width = this.Width;

        // *** Add the stylesheet only once
        if (Context.Items["StyleAdded"] == null)
        {
            HtmlLink link = new HtmlLink();
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            link.Attributes.Add("href", "~/csharp.css");
            this.Page.Header.Controls.Add(link);
            Context.Items["StyleAdded"] = "1";
        }
    }

    public enum DisplayStates
    {
        Button,
        Code
    }

    protected void btnShowCode_Click(object sender, EventArgs e)
    {
        DisplayCode();
    }

    protected void DisplayCode()
    {
            string File = Server.MapPath(this.ResolveUrl(this.CodeFile));
            File = File.ToLower();

            // *** Allow only source and aspx files
            if (File.IndexOf(".cs") < 0 && File.IndexOf(".aspx") < 0 && File.IndexOf(".vb") < 0 && File.IndexOf(".asmx") < 0 && File.IndexOf(".ashx") < 0)
            {
                this.txtOutput.Text = "Invalid Filename specified...";
                return;
            }

            if (System.IO.File.Exists(File))
            {
                StreamReader sr = new StreamReader(File);
                string FileOutput = sr.ReadToEnd();
                sr.Close();


                if (File.ToLower().EndsWith(".cs") || File.ToLower().EndsWith(".asmx") || File.ToLower().EndsWith(".ashx"))
                {
                    CSharpFormat Format = new CSharpFormat();
                    this.txtOutput.Text =  "<div class='showcode'>" + Format.FormatCode(FileOutput) + "</div>";
                }
                else
                {
                    HtmlFormat Format = new HtmlFormat();
                    this.txtOutput.Text = "<div class='showcode'>" + Format.FormatCode(FileOutput) + "</div>";
                }
                
                //this.txtOutput.Text = "<pre>" + Server.HtmlEncode(FileOutput) + "</pre>";
            }

            this.btnShowCode.Visible = true;
            this.txtOutput.Visible = true;
    }

}
