using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Data.SqlClient;
using digioz.VideoModule;
using System.Net;
using System.Security.AccessControl;
using System.Text.RegularExpressions;
using digioz;

public partial class controls_VideoViewer : System.Web.UI.UserControl
{
    public string csVidName;
    public VideoInfo coVideoInfo;
    public string csVidPath = String.Empty;
    public string csUsername = string.Empty;
	public PortalSettings coPortalSettings;
    public string csCategoryName;
    public int ciCategoryID;

    public enum VideoDirection
    {
        Forward,
        Back
    }
    private void SetUserHyperLink(User poUser)
    {
        coPortalSettings = PortalSettingsManagement.GetPortalSettings();
        hypUsername.HRef = "../Default.aspx?page=" + Server.UrlEncode(digioz.Encryption64.Encrypt("Profile", coPortalSettings.URLEncryptionKey))
                                 + "&username=" + Server.UrlEncode(digioz.Encryption64.Encrypt(poUser.Username, coPortalSettings.URLEncryptionKey));
    }
	
     protected void Page_Load(object sender, EventArgs e)
     {
		     
         int liVideoID = -1;
		 User loUser = new User();
         string lsPhysicalVidPath = String.Empty;
         string lsUrlReferrer = Request.Url.ToString();

         try
         {
             liVideoID = Convert.ToInt32(Request.QueryString["content"]);
             coVideoInfo = VideoInfoManagement.GetVideoInfo(liVideoID);
             csVidPath = lsUrlReferrer.Substring(0, lsUrlReferrer.LastIndexOf("/")) + "/" + "Videos" + "/" + coVideoInfo.Serverfilename;
             lsPhysicalVidPath = GetPhysicalVidPath(coVideoInfo.Serverfilename);
             loUser = UserManagement.GetUserByID(Convert.ToInt32(coVideoInfo.Uploader));
			 SetUserHyperLink(loUser);
             csUsername = loUser.Username;
         } catch {}

         try
         {
             ciCategoryID = Convert.ToInt16(Request.QueryString["category"]);
             csCategoryName = CategoryInfoManagement.GetCategory(ciCategoryID).CategoryName;
         } catch { }

         if (Session["username"] == null)
         {
             btnFavorites.Enabled = false;
         }

         ViewState["MediaUrl"] = csVidPath;

         try
         {
             ltVideo.Text = this.MediaPlayer(csVidPath, this.FullSize);
         }
         catch (Exception ex)
         {
             lblError.Text = ex.ToString();
             phError.Visible = true;
         }
    }

     protected void btnPrev_Click(object sender, EventArgs e)
     {
         FlipVideo(VideoDirection.Back);
     }

     protected void btnNext_Click(object sender, EventArgs e)
     {
         FlipVideo(VideoDirection.Forward);
     }

     public void FlipVideo(VideoDirection poVideoDirection)
     {
         string lsDirection = String.Empty;
         string lsRedirect = String.Empty;
         int liNewVideoID = 0;

         if (poVideoDirection == VideoDirection.Forward)
         {
             lsDirection = "B";
         }
         else
         {
             lsDirection = "F";
         }

         liNewVideoID = VideoInfoManagement.FlipVideo(coVideoInfo.ID, lsDirection, csCategoryName);

         lsRedirect = "Default.aspx?id=" + Request.QueryString["id"] + "&content=" + liNewVideoID;

         if (csCategoryName != null && csCategoryName.Length > 0)
         {
             lsRedirect = lsRedirect + "&category=" + ciCategoryID.ToString();
         }

         Response.Redirect(lsRedirect);
     }    

    /// <summary>
    /// FullSize()
    /// </summary>      
    public Boolean FullSize
    {
        get
        {
            if (ViewState["FullSize"] != null)
                return Convert.ToBoolean(ViewState["FullSize"]);
            else
                return true;
        }
        set
        {
            ViewState["FullSize"] = value;
        }
    }

    /// <summary>
    /// MediaUrl()
    /// </summary>
    /// <param name="Session"></param>
    /// <param name="pg"></param>
    public String MediaUrl
    {
        get
        {
            if (ViewState["MediaUrl"] != null)
                return Convert.ToString(ViewState["MediaUrl"]);
            else
                return ""; //Default video
        }
        set
        {
            ViewState["MediaUrl"] = value;
            
        }
    }

    /// <summary>
    /// PlayerWidth()
    /// </summary>
    public String PlayerWidth
    {
        get
        {
            if (ViewState["Width"] != null)
                return Convert.ToString(ViewState["Width"]);
            else
                return "640";
        }        
        set
        {
            ViewState["Width"] = value;
        }
    }

    /// <summary>
    /// PlayerHeight()
    /// </summary>
    public String PlayerHeight
    {
        get
        {
            if (ViewState["Height"] != null)
                return Convert.ToString(ViewState["Height"]);
            else
                return "480";
        }
        set
        {
            ViewState["Height"] = value;
        }
    }

    /// <summary>
    /// StandByMessage()
    /// </summary>   
    public String StandByMessage
    {
        get
        {
            if (ViewState["StandByMessage"] != null)
                return Convert.ToString(ViewState["StandByMessage"]);
            else
                return "Please wait while the player inializes...";
        }
        set
        {
            ViewState["StandByMessage"] = value;
        }
    }

    /// <summary>
    /// AutoStart()
    /// </summary>    
    public Boolean AutoStart
    {
        get
        {
            if (ViewState["AutoStart"] != null)
                return Convert.ToBoolean(ViewState["AutoStart"]);
            else
                return true;
        }
        set
        {
            ViewState["AutoStart"] = value;
        }
    }

    /// <summary>
    /// Balance()
    /// </summary>    
    public String Balance
    {
        set
        {
            ViewState["Balance"] = value;
        }
        get
        {
            try
            {
                if (ViewState["Balance"] != null)
                    return Convert.ToString(ViewState["Balance"]);
                else
                    return "0";
            }
            catch
            {
                return "0";
            }
        }
    }

    /// <summary>
    /// CurrentPosition()
    /// </summary>   
    public Int32 Position
    {
        get
        {
            if (ViewState["CurrentPosition"] != null)
                return Convert.ToInt32(ViewState["CurrentPosition"]);
            else
                return 0;
        }
        set
        {
            ViewState["CurrentPosition"] = value;
        }
    }

    /// <summary>
    /// ShowControls()
    /// </summary>    
    public Boolean ShowControls
    {
        get
        {
            if (ViewState["ShowControls"] != null)
                return Convert.ToBoolean(ViewState["ShowControls"]);
            else
                return true;
        }
        set
        {
            ViewState["ShowControls"] = value;
        }
    }

    /// <summary>
    /// ContextMenu()
    /// </summary>   
    public Boolean ContextMenu
    {
        get
        {
            if (ViewState["ContextMenu"] != null)
                return Convert.ToBoolean(ViewState["ContextMenu"]);
            else
                return false;
        }
        set
        {
            ViewState["ContextMenu"] = value;
        }
    }

    /// <summary>
    /// Mute()
    /// </summary>    
    public Boolean Mute
    {
        get
        {
            if (ViewState["Mute"] != null)
                return Convert.ToBoolean(ViewState["Mute"]);
            else
                return false;
        }
        set
        {
            ViewState["Mute"] = value;
        }
    }

    /// <summary>
    /// PlayCount()
    /// </summary>  
    public Int32 PlayCount
    {
        get
        {
            if (ViewState["PlayCount"] != null)
                return Convert.ToInt32(ViewState["PlayCount"]);
            else
                return 1;
        }
        set
        {
            ViewState["PlayCount"] = value;
        }
    }

    /// <summary>
    /// Rate()
    /// </summary>   
    public Double Rate
    {
        get
        {
            if (ViewState["Rate"] != null)
               return Convert.ToDouble(ViewState["Rate"]);
            else
                return 1.0;
        }
        set
        {
            ViewState["Rate"] = value;
        }
    }

    /// <summary>
    /// UIMode()
    /// </summary>    
    public String UIMode
    {
        get
        {
            if (ViewState["UIMode"] != null)
                return Convert.ToString(ViewState["UIMode"]);
            else
                return "Full";
        }
        set
        {
            ViewState["UIMode"] = value;
        }
    }

    /// <summary>
    /// ShowDisplay()
    /// </summary>
    public Boolean ShowDisplay
    {
        get
        {
            if (ViewState["ShowDisplay"] != null)
               return Convert.ToBoolean(ViewState["ShowDisplay"]);
            else
                return false;
        }
        set
        {
            ViewState["ShowDisplay"] = value;
        }
    }

    /// <summary>
    /// Volume()
    /// </summary>    
    public Int32 Volume
    {
        get
        {
            if (ViewState["Volume"] != null)
               return Convert.ToInt32(ViewState["Volume"]);
            else
                return 50;
        }
        set
        {
            ViewState["Volume"] = value;
        }
    }

    /// <summary>
    /// MediaPlayer()
    /// </summary>   
    private string MediaPlayer(string MediaUrl, bool FullSize)
    {
        string digiPlayer = "";
        MediaUrl = MediaUrl + "";
        MediaUrl = MediaUrl.Trim();

        if (FullSize)
        {  
            this.PlayerWidth = "580";
            this.PlayerHeight = "450";
        }
        else
        {
            //continue 
        }

        digiPlayer = digiPlayer + "<object classid='CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95' id='player' width='" + this.PlayerWidth + "' height='" + this.PlayerHeight + "' standby= '" + this.StandByMessage + "'>";
        digiPlayer = digiPlayer + "<param name='url' value='" + this.MediaUrl + "' />";
        digiPlayer = digiPlayer + "<param name='src' value='" + this.MediaUrl + "' />";
        digiPlayer = digiPlayer + "<param name='autoStart' value='" + this.AutoStart + "' />";
        digiPlayer = digiPlayer + "<param name='balance' value='" + this.Balance + "' />";
        digiPlayer = digiPlayer + "<param name='currentPosition' value='" + this.Position + "' />";
        digiPlayer = digiPlayer + "<param name='showControls' value='" + this.ShowControls + "' />";
        digiPlayer = digiPlayer + "<param name='contextMenu' value='" + this.ContextMenu + "' />";
        digiPlayer = digiPlayer + "<param name='fullScreen' value='" + this.FullSize + "' />";
        digiPlayer = digiPlayer + "<param name='mute' value='" + this.Mute + "' />";
        digiPlayer = digiPlayer + "<param name='playCount' value='" + this.PlayCount + "' />";
        digiPlayer = digiPlayer + "<param name='rate' value='" + this.Rate + "' />";
        digiPlayer = digiPlayer + "<param name='uiMode' value='" + this.UIMode + "' />";
        digiPlayer = digiPlayer + "<param name='showDisplay' value='" + this.ShowDisplay + "' />";
        digiPlayer = digiPlayer + "<param name='volume' value='" + this.Volume + "' />";
        digiPlayer = digiPlayer + "</object>";

        return digiPlayer;
    }

    protected void btnFavorites_Click(object sender, EventArgs e)
    {
        string lsTableId = string.Empty;
        string lsVideoPath = string.Empty;
        string lsType = string.Empty;
        string lsVideoName = string.Empty;
        string lsVideoUrl = string.Empty;
        string lsUrlReferrer = Request.Url.ToString();
        
        lsType = "video";
        lsVideoPath = csVidPath;
        lsVideoName = Path.GetFileName(lsVideoPath);
        lsVideoUrl = lsUrlReferrer.Substring(0, lsUrlReferrer.LastIndexOf("/")) + "/" + "videos" + "/" + lsVideoName;

        VideoInfoManagement loVideoInfoManagement = new VideoInfoManagement();
        lsTableId = Convert.ToString(VideoInfoManagement.GetVideoIDFromName(lsVideoName));        
        loVideoInfoManagement.SaveToFavorites(lsType, lsTableId, lsVideoName);

    }

    protected void btnDownload_Click(object sender, EventArgs e)
    {       
        string videoPath = string.Empty;
        string videoName = string.Empty;        
        string securePath = string.Empty;        
        FileStream sourceFile;
        byte[] getContent;
        long FileSize;

        videoPath = csVidPath;
        videoName = Path.GetFileName(videoPath); 
        videoPath = GetPhysicalVidPath(videoName);   
        securePath = videoPath;
        string ext = Path.GetExtension(videoName);
        ext = ext.Substring(1, ext.Length - 1);

        digioz.ExtensionsList loExtensionsList = digioz.ExtensionsManagement.GetExtensionsList();

        foreach (digioz.Extensions loExtension in loExtensionsList)
        {
            if (ext == loExtension.Name)
            {
                Response.ContentType = loExtension.MimeType;
                Response.AddHeader("content-disposition", "attachment; filename=" + videoName);
                sourceFile = new FileStream(securePath, FileMode.Open, FileAccess.Read);
                FileSize = sourceFile.Length;
                getContent = new byte[(int)FileSize];
                sourceFile.Read(getContent, 0, (int)sourceFile.Length);
                sourceFile.Close();
                Response.BinaryWrite(getContent);
                break;
            }
        }
    }

    public string GetPhysicalVidPath(string psFileName)
    {
        string physicalPath = Request.PhysicalPath;
        physicalPath = physicalPath.Replace("Default.aspx", "videos");
        string lsFullPath = physicalPath + "\\" + psFileName;
        return lsFullPath;
    }    
}