using System;
using System.Data;
using Oracle.ManagedDataAccess.Client;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
public partial class Masters_TA_UI_OrclConfig : Base.BasePage
{
    #region "Variable Declaration"
    SMSNotification objSMS = new SMSNotification();
    ResourceManager rm;
    CultureInfo ci;
    int empAutoID = 0;
    Cls_CheckUser objCheckUser = new Cls_CheckUser();
    DataSet ds_Checkuser = new DataSet();
    cls_Common objcommon = new cls_Common();
    EmployeeEventData objEmployee = new EmployeeEventData();
    #endregion
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        //Set UserRights
        if (!IsPostBack)
        {
            objcommon.menu("Configuration", "310");
            UserRights();
            LoadStrings(ci);
            BindOracleDatabaseDetails();
        }
        site.SiteMapProvider = Convert.ToString(Session["SiteMapProvider"]);
        empAutoID = Convert.ToInt32(Convert.ToString(Session["EmpAutoID"]));

    }
    private void LoadStrings(CultureInfo ci)
    {
        btn_save.Text = "&nbsp;&nbsp;" + rm.GetString("btn_Save", ci);
        lbl_Header.Text = "Oracle Database Configuration";
        lbl_Man.Text = rm.GetString("IndicatesMandatoryFields", ci);
    }
    private void UserRights()
    {
        //for Vaighai client
        ds_Checkuser = objCheckUser.checkRights(310, Convert.ToInt32(Session["userGroup_Id"]), "");
        if (ds_Checkuser.Tables[0].Rows.Count > 0)
        {
            if (ds_Checkuser.Tables[0].Rows[0]["addPrv"] != DBNull.Value && Convert.ToInt32(ds_Checkuser.Tables[0].Rows[0]["addPrv"]) == 1)
            {
                this.btn_save.Visible = true;
            }
            else
            {
                this.btn_save.Visible = false;
            }
        }
        else
        {
            Response.Redirect("MasterLandingPage.aspx");
        }
    }
    private void BindOracleDatabaseDetails()
    {
        DataSet dsOracleDB = new DataSet();
        if (Convert.ToString(ViewState["OracleDBDetails"]) == "")
        {
            dsOracleDB = objSMS.GetOracleDbDetails();
            ViewState["OracleDBDetails"] = dsOracleDB;
        }
        else
        {
            dsOracleDB = (DataSet)ViewState["OracleDBDetails"];
        }
        if (dsOracleDB.Tables.Count > 0)
        {
            if (dsOracleDB.Tables[0].Rows.Count > 0)
            {
                if (Convert.ToString(dsOracleDB.Tables[0].Rows[0]["HostName"]) != "")
                {
                    txthostName.Text = (Convert.ToString(dsOracleDB.Tables[0].Rows[0]["HostName"]));
                }
                if (Convert.ToString(dsOracleDB.Tables[0].Rows[0]["Port"]) != "")
                {
                    txtPort.Text = (Convert.ToString(dsOracleDB.Tables[0].Rows[0]["Port"]));
                }
                if (Convert.ToString(dsOracleDB.Tables[0].Rows[0]["ServiceName"]) != "")
                {
                    txtServiceName.Text = (Convert.ToString(dsOracleDB.Tables[0].Rows[0]["ServiceName"]));
                }
                if (Convert.ToString(dsOracleDB.Tables[0].Rows[0]["UserName"]) != "")
                {
                    txtUserName.Text = (Convert.ToString(dsOracleDB.Tables[0].Rows[0]["UserName"]));
                }
                if (Convert.ToString(dsOracleDB.Tables[0].Rows[0]["Pwd"]) != "")
                {
                    txtPwd.Text = (Convert.ToString(dsOracleDB.Tables[0].Rows[0]["Pwd"]));
                    txtPwd.Attributes["value"] = Convert.ToString(dsOracleDB.Tables[0].Rows[0]["Pwd"]);
                    txtPwd.Attributes.Add("value", Convert.ToString(dsOracleDB.Tables[0].Rows[0]["Pwd"]));
                }
            }
        }
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        string strScript = "";
        if (Convert.ToString(txthostName.Text) == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Hostname');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (Convert.ToString(txtPort.Text) == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Port value');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (Convert.ToString(txtServiceName.Text) == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Service name');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (Convert.ToString(txtUserName.Text) == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Username');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (Convert.ToString(txtPwd.Text) == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Password');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else
        {
            try
            {
                objSMS.InsertOracleDatabaseDetails
                    (
                    Convert.ToString(txthostName.Text),
                    Convert.ToString(txtPort.Text),
                    Convert.ToString(txtServiceName.Text),
                    Convert.ToString(txtUserName.Text),
                    Convert.ToString(txtPwd.Text)
                        );
                ViewState["OracleDBDetails"] = "";
                BindOracleDatabaseDetails();
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('Oracle Database Configuration is saved successfully');";
                strScript = strScript + "</script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
            }
            catch (Exception ex)
            {
                base.Message(ex.ToString());
            }
        }
    }

    protected void btnTest_Click(object sender, EventArgs e)
    {
        string strScript = "";
        if (Convert.ToString(txthostName.Text) == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Hostname');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (Convert.ToString(txtPort.Text) == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Port value');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (Convert.ToString(txtServiceName.Text) == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Service name');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (Convert.ToString(txtUserName.Text) == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Username');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else if (Convert.ToString(txtPwd.Text) == "")
        {
            strScript = strScript + "<script language='javascript'>";
            strScript = strScript + "alert('Enter Password');";
            strScript = strScript + "</script>";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
        }
        else
        {
            try
            {
                string oradb = "Data Source=(DESCRIPTION =" + "(ADDRESS = (PROTOCOL = TCP)(HOST = " + Convert.ToString(txthostName.Text) + ")(PORT = " + Convert.ToString(txtPort.Text) + "))"
                + "(CONNECT_DATA =" + "(SERVER = DEDICATED)" + "(SERVICE_NAME = " + Convert.ToString(txtServiceName.Text) + ")));" + "User Id=" + Convert.ToString(txtUserName.Text) + ";Password=" + Convert.ToString(txtPwd.Text) + ";";
                OracleConnection conn = new OracleConnection(oradb);
                //string query = "insert into studentinfo values(787)";
                conn.Open();
                //for (int i = 0; i < 1000; i++)
                //{
                //    query = "insert into studentinfo values(" + Convert.ToString(i) + ")";
                //    //Console.Write("Connected to Oracle" + conn.ServerVersion);
                //    OracleCommand cmd = new OracleCommand();
                //    cmd.CommandText = query;
                //    cmd.Connection = conn;
                //    cmd.ExecuteNonQuery();
                //}
                conn.Close();
                conn.Dispose();
                strScript = strScript + "<script language='javascript'>";
                strScript = strScript + "alert('Connected Successfully');";
                strScript = strScript + "</script>";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", strScript, false);
            }
            catch (Exception ex)
            {
                base.Message(ex.ToString());
            }
        }
    }
}