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;

public partial class Business_Archive : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SecurityAgent.AuthenticateArchive();
            getNumberOfDays();
            loadJobs();
        }
        lblError.Text = "";
    }

    private void loadJobs()
    {
        //string DBConnection = Helper.dbConnectionLive();

        //OracleConnection oracon = new OracleConnection(DBConnection);
        //oracon.Open();

        //OracleCommand oracom = new OracleCommand();
        //oracom.Connection = oracon;

        //oracom.CommandType = CommandType.StoredProcedure;
        //oracom.CommandText = "archive_db.display_archive_status";

        //oracom.Parameters.Add("p_status", OracleType.Cursor).Direction = ParameterDirection.Output;

        //OracleDataAdapter da = new OracleDataAdapter(oracom);
        //DataSet ds = new DataSet();
        //da.Fill(ds);

        //GridView1.DataSource = ds;
        //GridView1.DataBind();

        //oracon.Close();
    }

    private void getEstimate()
    {
        string DBConnection = Helper.dbConnectionLive();

        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

        OracleCommand oracom = new OracleCommand();
        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "archive_db.estimate_num_recs";

        oracom.Parameters.Add("recs_num", OracleType.Number).Direction = ParameterDirection.Output;

        oracom.ExecuteNonQuery();

        oracon.Close();
    }

    private void getNumberOfDays()
    {
        string DBConnection = Helper.dbConnectionLive();

        OracleConnection oracon = new OracleConnection(DBConnection);
        oracon.Open();

        OracleCommand oracom = new OracleCommand();
        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;
        oracom.CommandText = "archive_db.get_archive_par";

        oracom.Parameters.Add("par1", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("par2", OracleType.Number).Direction = ParameterDirection.Output;
        oracom.Parameters.Add("par3", OracleType.Number).Direction = ParameterDirection.Output;


        OracleDataAdapter da = new OracleDataAdapter(oracom);
        DataSet ds = new DataSet();
        da.Fill(ds);

        string days = oracom.Parameters["par1"].Value.ToString();
        ViewState["min"] = oracom.Parameters["par2"].Value.ToString();
        string estimate = oracom.Parameters["par3"].Value.ToString();

        oracon.Close();

        txtDays.Text = days;
        txtRecords.Text = estimate;
    }

    private void setNumberOfDays()
    {
        string DBConnection = Helper.dbConnectionLive();

        OracleConnection oracon = new OracleConnection(DBConnection);

        oracon.Open();

        OracleCommand oracom    = new OracleCommand();

        oracom.Connection       = oracon;

        oracom.CommandType      = CommandType.StoredProcedure;

        oracom.CommandText      = "archive_db.update_archive_par";

        oracom.Parameters.Add( "par2" , OracleType.Number ).Direction = ParameterDirection.Input;

        oracom.Parameters[ "par2" ].Value = txtDays.Text;

        oracom.ExecuteNonQuery();

        oracon.Close();
    }

    private void startArchive()
    {
        string DBConnection = Helper.dbConnectionLive();

        OracleConnection oracon = new OracleConnection(DBConnection);

        oracon.Open();

        OracleCommand oracom = new OracleCommand();

        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;

        oracom.CommandText = "archive_db.start_archive_process";

        oracom.ExecuteNonQuery();

        oracon.Close();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtDays.Text.Length == 0)
        {
            lblError.Text = "please enter a number";
        }
        else
        {
            int days = int.Parse(txtDays.Text);
            int limit = int.Parse(ViewState["min"].ToString());
            if (days < limit)
                lblError.Text = "Number of days must be bigger than : " + limit;
            else
            {
                setNumberOfDays();
                lblError.Text = "Number of days saved";
                getEstimate();
                getNumberOfDays();
            }
        }
    }

    protected void btnEnable_Click(object sender, EventArgs e)
    {

        enableArchive();
    }

    protected void btnDisable_Click(object sender, EventArgs e)
    {

        disableArchive();
    }


    protected void btnArchive_Click(object sender, EventArgs e)
    {
        startArchive();
        lblError.Text = "Arhive started";
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        loadJobs();
    }

    private void enableArchive()
    {
        string DBConnection = Helper.dbConnectionLive();

        OracleConnection oracon = new OracleConnection(DBConnection);

        oracon.Open();

        OracleCommand oracom = new OracleCommand();

        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;

        oracom.CommandText = "archive_db.enable_Archive_schedule";

        oracom.ExecuteNonQuery();

        oracon.Close();
    }

    private void disableArchive()
    {
        string DBConnection = Helper.dbConnectionLive();

        OracleConnection oracon = new OracleConnection(DBConnection);

        oracon.Open();

        OracleCommand oracom = new OracleCommand();

        oracom.Connection = oracon;

        oracom.CommandType = CommandType.StoredProcedure;

        oracom.CommandText = "archive_db.disable_Archive_schedule";

        oracom.ExecuteNonQuery();

        oracon.Close();
    }

}
