using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Data.OracleClient;
using System.Runtime.InteropServices;
using System.Net.Mail;
using System.Collections;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;

namespace Merchant_Email
{
    class Program
    {
        //OracleConnection conn;
        //static private string GetConnectionString()
        //{
        //    return "Data Source=ATMRecon;Persist Security Info=True;User ID=asr;Password=rsa#2331;Unicode=True";
        //}

        //void Connect()
        //{
        //    conn = new OracleConnection();
        //    conn.ConnectionString = GetConnectionString();
        //    conn.Open();
        //    Console.WriteLine("Connected to Oracle" + conn.ServerVersion);
        //}

        //void Close()
        //{
        //    conn.Close();
        //    conn.Dispose();
        //}

        public class GV
        {
            public static string filedate, filename, parameters, response;
        }

        private bool startJob(string filedate, ref string error)
        {
            GV.parameters = "";
            try
            {
                string DBConnection = Helper.dbConnection();

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

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

                oracom.CommandType = CommandType.StoredProcedure;
                oracom.CommandText = "pos_settelment_monitor.Gen_Rpt_POSSettl";

                oracom.Parameters.Add("p_current_uid", OracleType.Number).Direction = ParameterDirection.Input;
                oracom.Parameters.Add("p_job_name", OracleType.VarChar).Direction = ParameterDirection.Input;
                oracom.Parameters.Add("p_file_date", OracleType.VarChar).Direction = ParameterDirection.Input;
                oracom.Parameters.Add("p_lang", OracleType.VarChar).Direction = ParameterDirection.Input;
                oracom.Parameters.Add("p_res_code", OracleType.Number).Direction = ParameterDirection.Output;
                oracom.Parameters.Add("p_type", OracleType.VarChar, 250).Direction = ParameterDirection.Output;
                oracom.Parameters.Add("p_res_msg", OracleType.VarChar, 250).Direction = ParameterDirection.Output;

                oracom.Parameters["p_job_name"].Value = "GEN_RPT_POSSETTL";
                GV.parameters = "p_job_name-GEN_RPT_POSSETTL:";

                oracom.Parameters["p_file_date"].Value = filedate;
                oracom.Parameters["p_lang"].Value = "EN";
                oracom.Parameters["p_current_uid"].Value = "1";

                oracom.ExecuteNonQuery();
                oracon.Close();
                string a = oracom.Parameters["p_res_code"].Value.ToString();
                error = oracom.Parameters["p_res_msg"].Value.ToString();
                if (a == "0")
                {
                    //ExceptionUtility.LogException(0, "pos_settelment_monitor.start_pos_settl_job", "354.1", "startJob", parameters);
                    return true;
                }
                else
                {
                    //ExceptionUtility.LogException(9, "pos_settelment_monitor.start_pos_settl_job", "354.1", "startJob", parameters);
                    return false;
                }
            }
            catch (Exception ex)
            {
                //ExceptionUtility.LogException(ex, "pos_settelment_monitor.start_pos_settl_job", "354.1", "startJob", parameters);
                //Response.Redirect("../ErrorPages/ErrorPage.aspx");
                return false;
            }
        }

        static void Main(string[] args)
        {
            Program me = new Program();
            //me.Connect();

            string[] reader = Console.ReadLine().Split(' ');
            if (reader != null)
            {
                GV.filedate = reader[0];
                GV.filename = reader[1];

                string error = "";
                //ExceptionUtility.LogException(0, "", "65.354", "btnStart_Click", "");
                bool saved = me.startJob(GV.filedate, ref error);
                if (saved)
                {
                    GV.response = "Process completed"; 
                }
                else
                {
                    if (Helper.OracleError(error))
                    {
                        GV.response = "An Error Occured!";
                        //ExceptionUtility.LogException(9, "", "65.354", "btnStart_Click", error);
                    }
                    else
                        GV.response = error;
                }

                //// Create service collection
                //var serviceCollection = new ServiceCollection();
                //ConfigureServices(serviceCollection);

                //// Create service provider
                //var serviceProvider = serviceCollection.BuildServiceProvider();

                //// Run app
                //serviceProvider.GetService<App>().Run();

                Console.Write("\nPress any key to continue... ");
                Console.ReadLine();
            }

            //me.Close();
        }

        //private static void ConfigureServices(IServiceCollection serviceCollection)
        //{
        //    // Build configuration
        //    var configuration = new ConfigurationBuilder()
        //        .SetBasePath(AppContext.BaseDirectory)
        //        .AddJsonFile("appsettings.json", false)
        //        .Build();

        //    // Add console logging
        //    serviceCollection.AddSingleton(new LoggerFactory()
        //        .AddConsole(configuration.GetSection("Logging"))
        //        .AddSerilog()
        //        .AddDebug());
        //    serviceCollection.AddLogging();

        //    // Add Serilog logging           
        //    Log.Logger = new LoggerConfiguration()
        //    .ReadFrom.Configuration(configuration)
        //    .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
        //    .WriteTo.RollingFile(configuration["Serilog:LogFile"])
        //    .CreateLogger();

        //    // Add access to generic IConfigurationRoot
        //    serviceCollection.AddSingleton(configuration);

        //    // Add the App
        //    serviceCollection.AddTransient<App>();
        //}
    }
}
