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.VideoModule;
using digioz;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using DexterLib;

public partial class controls_VideoUpload : System.Web.UI.UserControl
{
    const long MAX_LENGTH = 4000000;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Check for Login ----------------------------------------------------------

        if (Session["userid"] == null || Convert.ToString(Session["userid"]) == String.Empty)
        {
            Response.Redirect("Login.aspx");
        }

        if (!IsPostBack)
        {
            // Get all video categories
            ddlCategory.DataSource = CategoryInfoManagement.GetCategoriesList("Video");
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataBind();
        }
    }

    protected void btn_upload_Click(object sender, EventArgs e)
    {
        string lsExt;
        Extensions loExtension = null;

        if (!FileUpload1.HasFile)
        {
            Label1.CssClass = "messageError";
            Label1.Text = "Please select a file to upload";
            return;
        }

        HttpPostedFile loFile = (HttpPostedFile)(FileUpload1.PostedFile);
        if (loFile.ContentLength > MAX_LENGTH)
        {
            Label1.CssClass = "messageError";
            Label1.Text = "The file size exceeded limit . ";
            Label1.Font.Bold = true;
            Label1.ForeColor = System.Drawing.Color.Blue;
            return;
        }

        if (string.IsNullOrEmpty(TextBoxTitle.Text))
        {
            Label1.CssClass = "messageError";
            Label1.Text = "Please complete Title";
            return;
        }

        lsExt = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName.ToLower());

        if (!ExtensionsManagement.IsFileValid(lsExt, "VIDEO", out loExtension))
        {
            Label1.CssClass = "messageError";
            Label1.Text = "This file format is not accepted for upload !";
            return;
        }

        string lsFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
        string lsServerPath = Server.MapPath("~") + "\\videos\\" + lsFileName + lsExt;
        FileUpload1.PostedFile.SaveAs(lsServerPath);
        
        // Generate thumbnail picture --------------
        string lsThumbtailName = Server.MapPath("~") + "\\videos\\" + lsFileName;
        string lsFBitmapName = lsThumbtailName + ".bmp";
        string lsFJpegName = lsThumbtailName + ".jpg";

        try
        {
            MediaDetClass loMD = new MediaDetClass();
            loMD.Filename = lsServerPath;
            loMD.CurrentStream = 0;
            loMD.WriteBitmapBits(0, 150, 150, lsFBitmapName);

            System.Drawing.Image loImg = System.Drawing.Image.FromFile(lsFBitmapName);
            loImg.Save(lsFJpegName, ImageFormat.Jpeg);
            loImg.Dispose();
            System.IO.File.Delete(lsFBitmapName);
        }
        catch (Exception loEx)
        {
            // Means media not supported
        }
        // ----------------------------------------

        VideoInfo loRecord = new VideoInfo();
        loRecord.FileName = Utility.ConvertHtmlFilterInput(TextBoxTitle.Text);
        loRecord.Description = Utility.ConvertHtmlFilterInput(TextBoxDescription.Text);
        loRecord.Category = Utility.ConvertHtmlFilterInput(ddlCategory.SelectedValue);
        loRecord.Serverfilename = lsFileName + lsExt;
        loRecord.Uploadtime = DateTime.Now;
        loRecord.Uploader = Convert.ToString(Session["userid"]);

        if (null != loExtension)
        {
            loRecord.Mimetype = loExtension.MimeType;
        }
        else
        {
            loRecord.Mimetype = FileUpload1.PostedFile.ContentType;
        }

        if (VideoInfoManagement.SaveVideoInfo(loRecord, "New"))
        {
            Label1.CssClass = "messageOk";
            Label1.Text = "File " + loRecord.FileName + " was uploaded.";
        }
        else
        {
            Label1.CssClass = "messageError";
            Label1.Text = "File " + loRecord.FileName + "upload failed.";
        }
    }
}
