using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;

namespace ExWeb
{
    public partial class Mod_Password : Mod
    {
        public static string Help_Description =
            "Passworteingabe"
            + "\r\n"
            + "\r\nNicht zur eigenständigen Verwendung gedacht. Dieses Modul ist in allen anderen Modulen bereits enthalten."
            + "\r\nDas Modul setzt das Cookie 'authorize=1'."
            + "\r\nDas Modul wird implizit verwendet, indem in jedem anderen Modul das Attribut 'Authorize=1' gesetzt wird."
            + "\r\nDas Passwort kann in jedem über das Attribut 'Password' gesetzt werden."
            + "\r\nWenn dieses Password nicht angegeben ist, verwendet das Modul den Wert 'Password' aus config.sys.";

        public static string[][] Help_Attributes = new string[][]
        {
            // Attribut, Wert, DefaultWert, Optional, Hilfe
            new string[] {"Authozized", "true false", "false","","Gibt einem fragenden Programm den Autorisierungszustand an",""},
        };

        private bool _Authorized = false;
        public bool Authorized
        {
            get
            {
                return _Authorized;
            }
        }
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            SetPanelLayout(this.Controls);
        }
        protected new void Page_Load(object sender, EventArgs e)
        {
            Page.LoadComplete += new EventHandler(Page_LoadComplete);

            HttpCookie cookie = Request.Cookies["authorized"];
            if (cookie == null)
            {
                cookie = new HttpCookie("authorized");
                cookie.Secure = false;
                cookie.Value = "0";
                Response.Cookies.Add(cookie);
                this.Visible = true;
                TextBox_Password.Visible = true;
                Button_Password.Visible = true;
                Button_Reset.Visible = false;
                Label_Authorized.ForeColor = System.Drawing.Color.Red;
                _Authorized = false;
            }
            else
            {
                if (cookie.Value == "1")
                {
                    TextBox_Password.Visible = false;
                    Button_Password.Visible = false;
                    Button_Reset.Visible = true;
                    Label_Authorized.ForeColor = System.Drawing.Color.Green;
                    _Authorized = true;
                }
                else
                {
                    TextBox_Password.Visible = true;
                    Button_Password.Visible = true;
                    Button_Reset.Visible = false;
                    Label_Authorized.ForeColor = System.Drawing.Color.Red;
                    _Authorized = false;
                }
            }
            // Label_Cookies.Text = GetCookies(true);
            // Label_Cookies.Text += GetCookies(false);
        }
        protected void Button_Password_Click(object sender, EventArgs e)
        {
            NameValueCollection nvc = WebConfigurationManager.AppSettings;
            bool Authorized = false;
            if (base.Password!="") // standalone Aufruf in der Modul-Bibliothek
                Authorized = (Password == TextBox_Password.Text);
            if (!Authorized)
                if (((Mod)Parent).Password != "") // als Passwortmodul in Begleitung zu jedem anderen Modul
                    Authorized = (((Mod)Parent).Password == TextBox_Password.Text);
            if (!Authorized) // MasterPasswort in web.config
                Authorized = (nvc["Password"] == TextBox_Password.Text);
            if (Authorized)
            {
                Response.Cookies["authorized"].Value = "1";
                TextBox_Password.Visible = false;
                Button_Password.Visible = false;
                Button_Reset.Visible = true;
                Label_Authorized.ForeColor = System.Drawing.Color.Green;
                _Authorized = true;
            }
        }
        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            Response.Cookies["authorized"].Value = "0";
            Button_Password.Visible = true;
            TextBox_Password.Visible = true;
            Button_Reset.Visible = false;
            Label_Authorized.ForeColor = System.Drawing.Color.Red;
        }
        protected string GetCookies(bool bResponse)
        {
            int loop1; /*, loop2;*/
            HttpCookieCollection MyCookieColl;
            HttpCookie MyCookie;

            string s = "";
            MyCookieColl = (bResponse ? Response.Cookies : Request.Cookies);

            // Capture all cookie names into a string array.
            String[] arr1 = MyCookieColl.AllKeys;

            if (arr1.Length == 0) return "No " + (bResponse ? "Response" : "Request") + "-Cookies<br/>";
            // Grab individual cookie objects by cookie name. 
            for (loop1 = 0; loop1 < arr1.Length; loop1++)
            {
                MyCookie = MyCookieColl[arr1[loop1]];
                s += "<b>" + (bResponse ? "Response-" : "Request-") + "Cookie:</b> " + MyCookie.Name + "<br>";
                s += "Secure:" + MyCookie.Secure + "<br>";

                s += "Value:" + MyCookie.Value + "<br>";

                /*
                //Grab all values for single cookie into an object array.
                String[] arr2 = MyCookie.Values.AllKeys;

                //Loop through cookie Value collection and print all values. 
                for (loop2 = 0; loop2 < arr2.Length; loop2++)
                {
                    s += "Value" + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>";
                }
                 * */
            }
            return s;
        }
    }
}