using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using digioz;

namespace digioz
{
    public class ExtensionsManagement
    {
        private static PortalSettings coPortalSettings;

        public static ExtensionsList GetExtensionsList()
        {
            ExtensionsList loExtensionsList = new ExtensionsList();
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                Extensions loExtensions;
                DataTable loExtensionsTable;

                string lsSQL = "SELECT * FROM tblExtensions;";
                
                loDB.openConnection();
                loExtensionsTable = loDB.QueryDBDataset(lsSQL);
                loDB.closeConnection();

                foreach (DataRow loDR in loExtensionsTable.Rows)
                {
                    loExtensions = new Extensions();
                    loExtensions.ID = (int)loDR["ext_id"];
                    loExtensions.Name = (string)loDR["ext_name"];
                    loExtensions.Type = (string)loDR["ext_type"];

                    if (!loDR["ext_allowed"].Equals(DBNull.Value))
                    {
                        if (loDR["ext_allowed"].ToString() == "1")
                        {
                            loExtensions.Allowed = true;
                        }
                        else
                        {
                            loExtensions.Allowed = false;
                        }
                    }
                    loExtensions.MimeType = (string)loDR["ext_mimetype"];

                    loExtensionsList.Add(loExtensions);
                }
            }
            else
            {
                // Non Relational Database
            }
            return loExtensionsList;
        }

        /// <summary>
        /// Get an extension record based on file extension
        /// </summary>
        /// <param name="psFileExtension"></param>
        /// <returns></returns>
        public static Extensions GetExtension(string psFileExtension)
        {

            Extensions loExtension = null;
            coPortalSettings = PortalSettingsManagement.GetPortalSettings();

            if (coPortalSettings.DBRelational == true)
            {
                IDataRelational loDB = DataFactory.GetDataObject(coPortalSettings);
                DataTable loExtensionsTable;
                int liCount = 0;
                string lsSQL = "spGetExtension";
                System.Data.SqlClient.SqlParameter[] loParameters =
                        new System.Data.SqlClient.SqlParameter[1];
                loParameters[0] = new System.Data.SqlClient.SqlParameter(
                        "@ext_name", psFileExtension);

                try
                {
                    loDB.openConnection();
                    loExtensionsTable = loDB.QueryDBDatasetProc(lsSQL,
                                                            out liCount,
                                                            loParameters);
                    if (loExtensionsTable.Rows.Count > 0)
                    {
                        DataRow loDR = loExtensionsTable.Rows[0];
                        loExtension = new Extensions();

                        loExtension.ID = Convert.ToInt32(loDR["ext_id"]);
                        loExtension.Name = Convert.ToString(loDR["ext_name"]);
                        loExtension.Type = Convert.ToString(loDR["ext_type"]);

                        if (!loDR["ext_allowed"].Equals(DBNull.Value))
                        {
                            if (loDR["ext_allowed"].ToString() == "1")
                            {
                                loExtension.Allowed = true;
                            }
                            else
                            {
                                loExtension.Allowed = false;
                            }
                        }
                        loExtension.MimeType =
                                    Convert.ToString(loDR["ext_mimetype"]);
                    }
                }
                finally
                {
                    loDB.closeConnection();
                }
            }
            else
            {
                // Non Relational Database
            }
            return loExtension;
        }


        /// <summary>
        /// Validate file type based on file extension and values configured
        /// in the database
        /// </summary>
        /// <param name="psExt"></param>
        /// <param name="psFileType"></param>
        /// <returns></returns>
        public static bool IsFileValid(string psExt,
                                       string psFileType,
                                       out Extensions poExtension)
        {
            bool lbReturnValue = false;
            poExtension = null;

            string lsExtEx = psExt.Replace(".", string.Empty);
            Extensions loExtension = ExtensionsManagement.GetExtension(lsExtEx);

            // File type configured
            if  (null != loExtension)
            {
                // Extension is allowed and is of the expected file type
                if ((loExtension.Allowed) &&
                    (loExtension.Type == psFileType))
                {
                    lbReturnValue = true;
                    poExtension = loExtension;
                }
            }
            return lbReturnValue;
        }
    }
}
