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_ManageRSSFeeds : System.Web.UI.UserControl
{
    public bool Edit
    {
        set { ViewState["Edit"] = value; }
        get
        {
            if (ViewState["Edit"] == null)
                return false;
            else
                return (bool)ViewState["Edit"];
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (MultiView1.ActiveViewIndex == 1)    //list mode
        {
            PopulateGridView();
        }
        lblOutput.Text = " ";
    }

    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        MultiView1.ActiveViewIndex = Int32.Parse(e.Item.Value);
        if (MultiView1.ActiveViewIndex == 1)    //add/edit mode
        {
            PopulateGridView();
        }

        if (MultiView1.ActiveViewIndex == 0 && Edit == true)    //list view mode
        {
            btnPublish.Text = "Publish";
        }
    }

    protected void btnPublish_Click(object sender, EventArgs e)
    {
        string lsFeedValidationStatus = "";
        lsFeedValidationStatus = RSSFeedManagement.ValidateRSSFeed(txtChannelURL.Text);

        if (lsFeedValidationStatus.Substring(0, 5) != "err::")
        {
            RSSFeedManagement.SaveRssFeed(txtChannelDesc.Text, txtChannelURL.Text, ActiveEditFeed.Value, Session["username"].ToString(), Edit);
            if (Edit == true)
            {
                Edit = false;
                lblOutput.ForeColor = System.Drawing.Color.Green;
                lblOutput.Text = "The RSS Feed has been successfully updated to the database!";
                lblOutput.Font.Bold = true;
                Edit = false;
                txtChannelURL.Text = "";
                txtChannelDesc.Text = "";
            }
            else
            {
                lblOutput.ForeColor = System.Drawing.Color.Green;
                lblOutput.Text = "Operation succeeded!";
                lblOutput.Font.Bold = true;
            }
        }
        else
        {
            lblOutput.Text = "The Rss Feed has not been updated to the database.  " + lsFeedValidationStatus.Substring(5);
            lblOutput.ForeColor = System.Drawing.Color.Red;
            lblOutput.Font.Bold = true;
        }
    }

    protected void GridViewDisplay_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        this.GridViewDisplay.PageIndex = e.NewPageIndex;
        this.GridViewDisplay.DataBind();
    }

    protected void GridViewDisplay_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Visible = false;
            e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF'");
            // when mouse leaves the row, change the bg color to its original value   
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Visible = false;
        }
    }

    protected void GridViewDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete this feed?" + "')");
        }
    }

    protected void GridViewDisplay_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int _feedId = Convert.ToInt32(GridViewDisplay.DataKeys[e.RowIndex].Value);

        //delete article
        ActiveEditFeed.Value = _feedId.ToString();
        RSSFeedManagement.DeleteRssFeed(_feedId);
        PopulateGridView();
    }

    protected void PopulateGridView()
    {
        RSSFeedList loRssFeedList = RSSFeedManagement.FetchAllRSSFeeds();
        GridViewDisplay.DataSource = loRssFeedList;
        GridViewDisplay.DataBind();
    }

    protected void GridViewDisplay_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "btnChange")
        {
            int _feedId;
            _feedId = Convert.ToInt32(e.CommandArgument);
            MultiView1.ActiveViewIndex = 0;
            ActiveEditFeed.Value = _feedId.ToString();

            string[] lsRssFeedData = RSSFeedManagement.GetRssFeed(_feedId);
            txtChannelDesc.Text = lsRssFeedData[0];
            txtChannelURL.Text = Server.UrlDecode(lsRssFeedData[1]);
            btnPublish.Text = "Update ";
            Edit = true;
        }
    }


}


