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;

public partial class controls_admin_ChatroomManager : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Text = string.Empty;
        lblMessage.Visible = false;
        //string lsPattern = @"(?<begin>^.*)(?<query>&VideoId=\d{1,})(?<end>.*$)";
        //string lsPicturePattern = @"(?<begin>^.*)(?<query>&PictureId=\d{1,})(?<end>.*$)";

        //if (!IsPostBack)
        //{
        //    // Handle logic to navigate back to the referring page
        //    string lsPreviousPage = Request.QueryString["PreviousPage"];
        //    string lsBackText = string.Empty;
        //    string lsId = string.Empty;

        //    if (!string.IsNullOrEmpty(lsPreviousPage))
        //    {
        //        this.lnkBack.Visible = true;

        //        // if we were in the middle of editing a video record,
        //        // pass its Id to the referring url
        //        lsId = Request.QueryString["VideoId"];
        //        if (!string.IsNullOrEmpty(lsId))
        //        {
        //            if (lsPreviousPage.Contains("&VideoId="))
        //            {
        //                string lsReplace = string.Format("&VideoId={0}",
        //                                                 lsId);
        //                //lsPreviousPage = Regex.Replace(lsPreviousPage,
        //                //                               lsPattern,
        //                //                               "${begin}" + lsReplace + "${end}");
        //            }
        //            else
        //            {
        //                lsPreviousPage = string.Format("{0}&VideoId={1}",
        //                                              lsPreviousPage, lsId);
        //            }
        //        }
        //        else
        //        {
        //            lsId = Request.QueryString["PictureId"];
        //            if (!string.IsNullOrEmpty(lsId))
        //            {
        //                if (lsPreviousPage.Contains("&PictureId="))
        //                {
        //                    string lsReplace = string.Format("&PictureId={0}",
        //                                                     lsId);
        //                    //lsPreviousPage = Regex.Replace(lsPreviousPage,
        //                    //                               lsPicturePattern,
        //                    //                               "${begin}" + lsReplace + "${end}");
        //                }
        //                else
        //                {
        //                    lsPreviousPage = string.Format("{0}&PictureId={1}",
        //                                                  lsPreviousPage, lsId);
        //                }
        //            }
        //        }

        //        this.lnkBack.NavigateUrl = lsPreviousPage;
        //        lsBackText = Request.QueryString["PreviousTitle"];
        //        if (!string.IsNullOrEmpty(lsBackText))
        //        {
        //            this.lnkBack.Text = string.Format("Back to {0}", lsBackText);
        //        }
        //    }

        if (!IsPostBack)
        {
            this.BindData();

            //Default to Add Smiley
            SmileyManagerMenu.Items[0].Selected = true;
            this.SetSmileyMode("New");
            txtSmileyAbbr.Text = String.Empty;
            txtSmileyImage.Text = String.Empty;
        }
        //}
    }

    /// <summary>
    /// Activate view based on menu item clicked
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SmileyManagerMenu_MenuItemClick(object sender, MenuEventArgs e)
    {
        SmileyManagerMultiView.ActiveViewIndex =
            Convert.ToInt32(e.Item.Value);

        switch (e.Item.Value)
        {
            case "0":
                this.SetSmileyMode("New");
                txtSmileyAbbr.Text = String.Empty;
                txtSmileyImage.Text = String.Empty;
                break;
            case "1":
                this.BindData();
                break;
        }
    }

    protected void btnNewSmiley_Click(object sender, EventArgs e)
    {
        this.SetSmileyMode("New");
    }

    /// <summary>
    /// Save Smiley to the database
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveSmiley_Click(object sender, EventArgs e)
    {
        if (SmileyNameReqValidator.IsValid)
        {
            bool lbRetCode;
            string lsError = string.Empty;
            bool lbNewMode = (String.Compare(btnSaveSmiley.CommandArgument, "New") == 0);

            Smiley loSmiley = new Smiley();
            if (lbNewMode)
            {
                loSmiley.ID = -1;
            }
            else
            {
                loSmiley.ID = Convert.ToInt32(lblID.Text);
            }

            loSmiley.Abbr = txtSmileyAbbr.Text;
            loSmiley.Image = txtSmileyImage.Text;

            lbRetCode = SmileyManagement.Save(loSmiley, out lsError);
            lblMessage.Visible = true;

            if (lbRetCode == true)
            {
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Smiley {0} successfully",
                                                (lbNewMode) ? "added." : "updated.");

                if (lbNewMode)
                {
                    txtSmileyAbbr.Text = string.Empty;
                }
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Failed to {0} Smiley. Details: {1}",
                                                (lbNewMode) ? "add." : "update.", lsError);
            }

        }

    }
    protected void gvSmilies_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

    protected void gvSmilies_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }

    protected void gvSmilies_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.CommandArgument.ToString()))
        {
            gvSmilies.SelectedIndex = Convert.ToInt32(e.CommandArgument);
        }

        if (String.Compare(e.CommandName, "Edit", true) == 0)
        {
            // Display UI for Adding/Editing a Smiley
            this.SetSmileyMode("Update");
        }
        else if (String.Compare(e.CommandName, "Delete", true) == 0)
        {
            bool lbRetCode = false;
            string lsError = string.Empty;
            int liID = Convert.ToInt32(gvSmilies.SelectedValue);

            lbRetCode = SmileyManagement.Delete(liID, out lsError);

            // Reset Smiley list after deleting
            this.BindData();
            lblMessage.Visible = true;
            if (lbRetCode == true)
            {
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Smiley deleted successfully. ");
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Font.Bold = true;
                lblMessage.Text = string.Format("Failed to delete Smiley.");

                lblMessage.Text += " ";
                lblMessage.Text += lsError;
            }
        }

    }

    private void BindData()
    {
        string lsError = String.Empty;
        SmileyList loSmileyList = SmileyManagement.GetSmileyList(out lsError);
        gvSmilies.DataSource = loSmileyList;
        gvSmilies.DataBind();
    }

    /// <summary>
    /// Put "form" in either "Add" or "Edit" mode
    /// </summary>
    /// <param name="psMode"></param>
    private void SetSmileyMode(string psMode)
    {
        if (SetSmileyDetails(psMode))
        {
            // Activate the "Add/Update" view
            SmileyManagerMultiView.ActiveViewIndex = 0;
            btnSaveSmiley.CommandArgument = psMode;
            btnSaveSmiley.Text = "Update";
        }
    }

    /// <summary>
    /// Initialize controls for adding/editing a Smiley
    /// </summary>
    private bool SetSmileyDetails(string psMode)
    {
        bool lbRetValue = true;
        if (string.Compare(psMode, "Update", true) == 0)
        {
            Smiley loSmiley = SmileyManagement.GetSmiley(Convert.ToInt32(gvSmilies.SelectedValue));

            if (loSmiley != null)
            {
                lblID.Text = loSmiley.ID.ToString();
                txtSmileyAbbr.Text = loSmiley.Abbr;
                txtSmileyImage.Text = loSmiley.Image;
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = string.Format("Error loading Smiley.");

                // signal that we failed
                lbRetValue = false;
            }
        }
        else
        {
            lblID.Text = "-1";
            txtSmileyAbbr.Text = string.Empty;
        }

        return lbRetValue;
    }


    protected void gvSmilies_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvSmilies.PageIndex = e.NewPageIndex;
        this.BindData();
    }

    protected void gvSmilies_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
