using System;
using System.Configuration;
using System.Web.UI.WebControls;
using digioz;
using digioz.VideoModule;

public partial class controls_Videos : System.Web.UI.UserControl
{
    public string lsVideoPath = string.Empty;
    public string lsVideoServerName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string csURLEncryptionKey;

            csURLEncryptionKey = ConfigurationManager.AppSettings["URLEncryptKey"];

            hypPictureUpload.NavigateUrl = "../Default.aspx?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Video Upload", csURLEncryptionKey));

            categoriesDropDownList.DataSource = CategoryInfoManagement.GetCategoriesList("Video");
            categoriesDropDownList.DataBind();
            categoriesDropDownList.Items.Insert(0, new ListItem("Select a Category"));
            categoriesDropDownList.Items.Insert(1, new ListItem("All Categories"));
            categoriesDropDownList.Items.Insert(2, new ListItem("Default Category"));

            if (Request.QueryString["category"] != null && Utility.IsNumeric(Request.QueryString["category"]))
            {
                int liSelectedCategory = Convert.ToInt32(Request.QueryString["category"]);
                string lsCategorySelected = CategoryInfoManagement.GetCategory(liSelectedCategory).CategoryName;
                categoriesDropDownList.SelectedValue = lsCategorySelected;
            }
            BindData();
        }
    }

    private void BindData()
    {
        string categoryName = String.Empty;

        if (categoriesDropDownList.SelectedIndex != -1
            && categoriesDropDownList.SelectedIndex != 0
            && categoriesDropDownList.SelectedIndex != 1)
        {
            categoryName = categoriesDropDownList.SelectedItem.Text;
        }

        int currentpage = 1;
        if (!(string.IsNullOrEmpty(LabelPageNum1.Text)))
        {
            currentpage = Convert.ToInt32(LabelPageNum1.Text);
        }
        else
        {
            LabelPageNum1.Text = "1";
            LabelPageNum2.Text = "1";
        }
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = digioz.VideoModule.VideoInfoManagement.GetVideoInfoList(categoryName);
        ps.AllowPaging = true;
        ps.PageSize = 9;
        ps.CurrentPageIndex = currentpage - 1;
        ButtonFirstPage1.Enabled = true;
        ButtonPrePage1.Enabled = true;
        ButtonNextPage1.Enabled = true;
        ButtonLastPage1.Enabled = true;
        ButtonFirstPage2.Enabled = true;
        ButtonPrePage2.Enabled = true;
        ButtonNextPage2.Enabled = true;
        ButtonLastPage2.Enabled = true;
        if (currentpage == 1)
        {
            ButtonFirstPage1.Enabled = false;
            ButtonPrePage1.Enabled = false;
            ButtonFirstPage2.Enabled = false;
            ButtonPrePage2.Enabled = false;
        }
        if (currentpage == ps.PageCount)
        {
            ButtonNextPage1.Enabled = false;
            ButtonNextPage2.Enabled = false;
            ButtonLastPage1.Enabled = false;
            ButtonLastPage2.Enabled = false;
        }
        LabelPageAmount2.Text = Convert.ToString(ps.PageCount);
        LabelPageAmount1.Text = Convert.ToString(ps.PageCount);
        DataList1.DataSource = ps;
        DataList1.DataBind();
    }

    protected void ButtonFirstPage1_Click(object sender, EventArgs e)
    {
        NavigateToFirstPage();
    }

    protected void ButtonFirstPage2_Click(object sender, EventArgs e)
    {
        NavigateToFirstPage();
    }
    protected void ButtonPrePage1_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1.Text) - 1);
        LabelPageNum2.Text = LabelPageNum1.Text;
        BindData();
    }
    protected void ButtonPrePage2_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1.Text) - 1);
        LabelPageNum2.Text = LabelPageNum1.Text;
        BindData();
    }
    protected void ButtonNextPage1_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1.Text) + 1);
        LabelPageNum2.Text = LabelPageNum1.Text;
        BindData();
    }
    protected void ButtonNextPage2_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = Convert.ToString(Convert.ToInt32(LabelPageNum1.Text) + 1);
        LabelPageNum2.Text = LabelPageNum1.Text;
        BindData();
    }
    protected void ButtonLastPage1_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = LabelPageAmount1.Text;
        LabelPageNum2.Text = LabelPageAmount1.Text;
        BindData();
    }
    protected void ButtonLastPage2_Click(object sender, EventArgs e)
    {
        LabelPageNum1.Text = LabelPageAmount1.Text;
        LabelPageNum2.Text = LabelPageAmount1.Text;
        BindData();
    }

    public void VideoButton_Command(object sender, CommandEventArgs e)
    {
        int liPageID;

        if (e.CommandName == "VideoPath")
        {
            lsVideoPath = e.CommandArgument.ToString();

            int liVidPathStart = lsVideoPath.IndexOf("videos/") + 7;
            lsVideoServerName = lsVideoPath.Substring(liVidPathStart, lsVideoPath.Length - liVidPathStart);

            string lsFullPath = lsVideoServerName;
            Session["FullPath"] = lsFullPath;

            string lsPageName = "VideoViewer";
            if (lsPageName != null && lsPageName != String.Empty)
            {
                liPageID = MenuItemManagement.GetMenuItemFromMenuName(lsPageName).ID;
            }
            else
            {
                liPageID = 1;
            }

            int liVideoID = VideoInfoManagement.GetVideoIDFromName(lsVideoServerName);
            int liCategoryID = -1;

            if (categoriesDropDownList.SelectedIndex > 2)
            {
                liCategoryID = (CategoryInfoManagement.GetCategoryBasedOnName(categoriesDropDownList.SelectedItem.ToString())).ID;
            }

            string lsRedirect = "Default.aspx?id=" + liPageID + "&content=" + liVideoID; ;

            if (liCategoryID > -1)
            {
                lsRedirect = lsRedirect + "&category=" + liCategoryID.ToString();
            }

            Response.Redirect(lsRedirect);
        }
    }
    protected void categoriesDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        NavigateToFirstPage();
    }
    private void NavigateToFirstPage()
    {
        LabelPageNum1.Text = "1";
        LabelPageNum2.Text = "1";
        BindData();
    }
}
