using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
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 Spaces;
public partial class Admin_UserDefined : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UserLinkView.RowDeleting += new GridViewDeleteEventHandler(UserLinkView_RowDeleting);
        UserLinkView.RowEditing += new GridViewEditEventHandler(UserLinkView_RowEditing);
        UserLinkView.RowCancelingEdit += new GridViewCancelEditEventHandler(UserLinkView_RowCancelingEdit);
        UserLinkView.RowUpdating += new GridViewUpdateEventHandler(UserLinkView_RowUpdating);
        if (!IsPostBack)
            BindList();
    }

    void UserLinkView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        HiddenField hID = UserLinkView.Rows[e.RowIndex].FindControl("LinkID") as HiddenField;
        TextBox linkName = UserLinkView.Rows[e.RowIndex].FindControl("LinkName") as TextBox;
        TextBox urlAddress = UserLinkView.Rows[e.RowIndex].FindControl("UrlAddress") as TextBox;
        CheckBox newWindow = UserLinkView.Rows[e.RowIndex].FindControl("NewWindow") as CheckBox;
        if (hID != null)
        {
            int linkID = int.Parse(hID.Value);
            UserLink ul = UserLinks.FindLink(Cacher.GetUserConfig(Account).Links, linkID);
            if (ul == null)
                ul = new UserLink();
            ul.LinkID = linkID;
            ul.LinkName = linkName.Text;
            ul.UrlAddress = urlAddress.Text;
            ul.LinkType = LinkType.Link;
            ul.NewWindow = newWindow.Checked;
            ul.Account = Account;
            ul.Description = txtDescription.Text;
            UserLinks.UpdateUserLink(ul);
        }
        UserLinkView.EditIndex = -1;
        BindList();
    }

    void UserLinkView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        UserLinkView.EditIndex = -1;
        BindList();
    }

    void UserLinkView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        UserLinkView.EditIndex = e.NewEditIndex;
        BindList();
    }

    void UserLinkView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        HiddenField hID = UserLinkView.Rows[e.RowIndex].FindControl("LinkID") as HiddenField;
        if (hID != null)
        {
            int linkID = int.Parse(hID.Value);
            UserLinks.DelUserLink(linkID);
            UserLinks.Remove(Cacher.GetUserConfig(Account).Links, linkID);
        }
        UserLinkView.EditIndex = -1;
        BindList();
    }

    private void BindList()
    {
        List<UserLink> list = UserLinks.GetUserLinks(Account);

        UserLinkView.DataSource = UserLinks.GetLinks(list, LinkType.Link);
        UserLinkView.DataBind();
        chkNewWindow.Text = Globals.Text["UserDefined_OpenNewWindow"];
        btnAdd.Text=Globals.Text["Add"];
    }



    string Account
    {
        get { return SpacesContext.Current.Context.User.Identity.Name; }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string linkName = txtLinkName.Text;
        string url = txtAddress.Text;
        bool newWindow = chkNewWindow.Checked;
        UserLink ul = new UserLink();
        ul.LinkName = linkName;
        ul.Account = Account;
        ul.UrlAddress = url;
        ul.NewWindow = newWindow;
        ul.LinkType = LinkType.Link;
        ul.Description = txtDescription.Text;
        UserLinks.CreateUserLink(ul);
        //Cacher.GetUserConfig(Account).Links.Add(ul);
        BindList();
    }
    protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        string linkName = txtLinkName.Text;
        string url = txtAddress.Text;
        bool newWindow = chkNewWindow.Checked;
        UserLink ul = new UserLink();
        ul.LinkName = linkName;
        ul.Account = Account;
        ul.UrlAddress = url;
        ul.NewWindow = newWindow;
        ul.LinkType = LinkType.Link;
        UserLinks.CreateUserLink(ul);
        Cacher.GetUserConfig(Account).Links.Add(ul);
        BindList();
    }
}
