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 System.Data.OracleClient;
using System.IO;

public partial class CPS_MD5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadFiles();
            loadFiles2();
            links.Visible = false;
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            string md5_dir = ConfigurationSettings.AppSettings["MD5EXE"];
            System.Diagnostics.Process.Start(ConfigurationSettings.AppSettings["MD5EXE"], 
                ddlFiles.SelectedValue);
            lblError.Text = "MD5 file generated successfully";

            bool finish = true;

            string filename, cps_p_dir, cps_iis;

            while (finish)
            {
                try
                {
                    filename = ddlFiles.SelectedValue.Insert(ddlFiles.SelectedValue.Length - 4, "-MD5");
                    cps_p_dir = ConfigurationSettings.AppSettings["CPS_P"];
                    cps_iis = ConfigurationSettings.AppSettings["CPS_P_IIS"];
                    File.Move(ConfigurationSettings.AppSettings["CPS_P"] + "\\" + ddlFiles.SelectedValue.Insert(ddlFiles.SelectedValue.Length - 4, "-MD5"), 
                        ConfigurationSettings.AppSettings["CPS_P_IIS"] + "\\" + ddlFiles.SelectedValue.Insert(ddlFiles.SelectedValue.Length - 4, "-MD5"));
                    finish = false;
                }
                catch (Exception exx)
                {

                }
            }

            finish = true;
            while (finish)
            {
                try
                {

                    File.Move(ConfigurationSettings.AppSettings["CPS_P"] + "\\" + ddlFiles.SelectedValue, 
                        ConfigurationSettings.AppSettings["CPS_P_IIS"] + "\\" + ddlFiles.SelectedValue);
                    finish = false;
                }
                catch (Exception exx)
                {

                }
            }


            lnkCPS.NavigateUrl = ConfigurationSettings.AppSettings["CPS_P_IIS_V"] + ddlFiles.SelectedValue;
            lnkMD5.NavigateUrl = ConfigurationSettings.AppSettings["CPS_P_IIS_V"] + ddlFiles.SelectedValue.Insert(ddlFiles.SelectedValue.Length - 4, "-MD5");
            
            
            
            
            //lnkCPS.NavigateUrl = ConfigurationSettings.AppSettings["CPS_P"] + "\\" + ddlFiles.SelectedValue;
            //lnkMD5.NavigateUrl = ConfigurationSettings.AppSettings["CPS_P"] + "\\" + ddlFiles.SelectedValue.Insert(ddlFiles.SelectedValue.Length - 4, "-MD5");
            links.Visible = true;

            loadFiles();
            loadFiles2();
        }
        catch (Exception ex)
        {
            lblError.Text = "File Not Generated. Error happened during the execution of the MD5 exe";
        }

    }

    private void loadFiles()
    {
        ddlFiles.Items.Clear();
        DirectoryInfo di = new DirectoryInfo(ConfigurationSettings.AppSettings["CPS_P"]);
        FileInfo[] rgFiles = di.GetFiles("*.CPS");
        foreach (FileInfo fi in rgFiles)
        {
            ddlFiles.Items.Add(fi.Name);
        }
    }

    private void loadFiles2()
    {
        ddlFiles2.Items.Clear();
        DirectoryInfo di = new DirectoryInfo(Server.MapPath("../CPS_FILES"));
        FileInfo[] rgFiles = di.GetFiles("*.CPS");
        foreach (FileInfo fi in rgFiles)
        {
            ddlFiles2.Items.Add(fi.Name);
        }
    }

    protected void btnGenLink_Click(object sender, EventArgs e)
    {
        lnk2.NavigateUrl = ConfigurationSettings.AppSettings["CPS_P_IIS_V"] + ddlFiles2.SelectedValue;
    }
}
