using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Sex
{
    public partial class Sex : System.Web.UI.Page
    {
        public class MyList : Dictionary<string, string[]>
        {
            const string PrefixForHeaderRow = "#!#";

            string Title;
            string TitleFirstColumn;
            string[] TitleOtherColumns;

            public MyList(string Title)
            {
                this.Title = Title;
                this.TitleFirstColumn = null;
                this.TitleOtherColumns = null;
            }
            public MyList(string Title, string TitleFirstColumn, string[] TitleOtherColumns)
            {
                this.Title = Title;
                this.TitleFirstColumn = TitleFirstColumn;
                this.TitleOtherColumns = TitleOtherColumns;
            }
            public MyList()
            {
                this.Title = null;
            }
            public void Add(string Key, string[] Values)
            {
                base.Add(PrefixForHeaderRow + Key, Values);
            }
            public void Add(string Key, string Value)
            {
                Add(Key, new string[] { Value });
            }
            public void AddTitle(string Title, int Colspan)
            {
                string[] Values = new string[Colspan];
                for (int i = 0; i < Values.Length; i++)
                {
                    Values[i] = PrefixForHeaderRow;
                }
                Add(Title, Values);
            }
            public Control GetData()
            {
                Panel p = new Panel();
                if (!string.IsNullOrWhiteSpace(Title))
                {
                    LiteralControl l = new LiteralControl("<h3>" + Title + "</h3>\r\n");
                    p.Controls.Add(l);
                }
                Table t = new Table();
                if (TitleFirstColumn != null)
                {
                    for (int i = 0; i < TitleOtherColumns.Length; i++)
                    {
                        TitleOtherColumns[i] = PrefixForHeaderRow + TitleOtherColumns[i];
                    }
                    t.Rows.Add(GetRow(TitleFirstColumn, TitleOtherColumns));
                }
                foreach (KeyValuePair<string, string[]> kvp in this)
                {
                    t.Rows.Add(GetRow(kvp.Key, kvp.Value));
                }
                p.Controls.Add(t);
                return p;
            }
            private static TableCell GetCell(string Content)
            {
                TableCell tc = new TableCell();
                Label lb = new Label();
                string c = Content == null ? "" : Content.ToString();
                bool isHeader = false;
                if (c.StartsWith(PrefixForHeaderRow))
                {
                    isHeader = true;
                    c = c.Substring(PrefixForHeaderRow.Length);
                }
                if (c.ToLower() == "false") c = "<span class='LB'>&#x25cb;</span>";
                if (c.ToLower() == "true") c = "<span class='LB'>&#x25cf;</span>";
                lb.Text = c;
                tc.Controls.Add(lb);
                tc.VerticalAlign = VerticalAlign.Top;
                tc.CssClass = isHeader ? "LH" : "";
                return tc;
            }
            public static TableRow GetRow(string Left, string Right)
            {
                return GetRow(Left, new string[] { Right });
            }
            public static TableRow GetRow(string Left, string[] Right)
            {
                TableRow tr = new TableRow();
                tr.Cells.Add(GetCell(Left));
                for (int i = 0; i < Right.Length; i++)
                {
                    tr.Cells.Add(GetCell(Right[i]));
                }
                return tr;
            }
        }
    }
}