using System;
using System.Data;
using System.Configuration;
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;
// add
using System.Xml;
using System.Text;
using System.Collections;
using System.Collections.Specialized;

/// <summary>
/// Summary description for PSecurity
/// </summary>
public class PSecurity
{
    private const string mcsUsersFile = "App_Data/Users.xml";
    public PSecurity()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public bool Login(string sUser, string sPassword)
    {
        StringDictionary Users = new StringDictionary();
        string sUsers;
        string sName, sValue;

        sUsers = GetUserList();
        String2Collection(sUsers, "\r\n", ",", Users );

        if (Users[sUser] == sPassword)
            return true;

        return false;
    }
    private int String2Collection(string sList, string sRowSep, string sColSep,NameValueCollection nv)
    {
        string[] saRowSep = new string[] { sRowSep };
        string[] saColSep = new string[] { sColSep };
        string[] saRows;
        string[] saCols;
        string sName, sValue;

        nv.Clear();
        saRows = sList.Split(saRowSep, StringSplitOptions.RemoveEmptyEntries);
        foreach (string sRow in saRows)
        {
            saCols = sRow.Split(saColSep, StringSplitOptions.None);
            if (saCols.GetLength(0) > 1)
            {
                sName = saCols[0];
                sValue = saCols[1];
                nv.Add(sName, sValue);
            }
        }
        return nv.Count;
    }
    private int String2Collection(string sList, string sRowSep, string sColSep, StringDictionary sd)
    {
        string[] saRowSep = new string[] { sRowSep };
        string[] saColSep = new string[] { sColSep };
        string[] saRows;
        string[] saCols;
        string sName, sValue;

        sd.Clear();
        saRows = sList.Split(saRowSep, StringSplitOptions.RemoveEmptyEntries);
        foreach (string sRow in saRows)
        {
            saCols = sRow.Split(saColSep, StringSplitOptions.None);
            if (saCols.GetLength(0) > 1)
            {
                sName = saCols[0];
                if (!sd.ContainsKey(sName))
                {
                    sValue = saCols[1];
                    sd.Add(sName, sValue);
                }
            }
        }
        return sd.Count;
    }

    private int String2Collection(string sList, string sSep, StringCollection sc)
    {
        string[] saSep = new string[] { sSep };
        string[] sa;

        sc.Clear();
        sa = sList.Split(saSep, StringSplitOptions.None);
        foreach (string sValue in sa)
            sc.Add(sValue);

        return sc.Count;
    }

    public string Null2String(string sInput)
    {
        return (sInput == null) ? "" : sInput;
    }
    public string GetUserList()
    {
        return GetAttributeNameValueStringList(mcsUsersFile, "Users", "User", "ID", "Password");
    }

    public string GetRoleList()
    {
        return GetAttributeNameValueStringList(mcsUsersFile, "Roles", "Role", "ID", "Name");
    }

    private string GetAttributeNameValueStringList(string sXMLFile, string sParent, string sElement, string sAttrName, string sAttrValue)
    {
        XmlTextReader rd = new XmlTextReader(sXMLFile);
        StringBuilder sb = new StringBuilder();
        bool bElementParent;
        string sName, sValue;

        sb.Length = 0;
        bElementParent = false;
        sName = "";
        sValue = "";
        while (rd.Read())
        {
            switch (rd.NodeType)
            {
                case XmlNodeType.Element:
                    if (rd.Name == sParent)
                    {
                        bElementParent = true;
                        sb.Length = 0;
                    }
                    else if (rd.Name == sElement)
                    {
                        if (bElementParent)
                        {
                            sName = Null2String(rd.GetAttribute(sAttrName));
                            if (sName.Length > 0)
                            {
                                sValue = Null2String(rd.GetAttribute(sAttrValue));
                                sb.AppendLine(sName + "," + sValue);
                            }
                        }
                    }
                    break;
            }
        }
        rd.Close();

        return sb.ToString();
    }

    private string GetAttributeStringList(string sXMLFile, string sParent, string sElement, string sAttr)
    {
        XmlTextReader rd = new XmlTextReader(sXMLFile);
        StringBuilder sb = new StringBuilder();
        bool bElementParent;
        string sValue;

        sb.Length = 0;
        bElementParent = false;
        sValue = "";
        while (rd.Read())
        {
            switch (rd.NodeType)
            {
                case XmlNodeType.Element:
                    if (rd.Name == sParent)
                    {
                        bElementParent = true;
                        sb.Length = 0;
                    }
                    else if (rd.Name == sElement)
                    {
                        if (bElementParent)
                        {
                            sValue = Null2String(rd.GetAttribute(sAttr));
                            if (sValue.Length>0)
                                sb.AppendLine(sValue + ",");
                        }
                    }
                    break;
            }
        }
        rd.Close();
        if (sb.Length > 0)
            sb.Remove(sb.Length - 2, 1);

        return sb.ToString();
    }


}