using System;
using System.Data.SqlClient;
using Microsoft.JSInterop;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using System.Data;
using Todo_App.Data;

namespace Todo_App.Pages
{
    public partial class Login
    {
        [Inject]
        NavigationManager navigationManager { get; set; }
        public string login_password { get; set; }
        public string login_email { get; set; }
        [Inject]
        IJSRuntime jSRuntime { get; set; }

        public string register_name { get; set; }
        public string register_surname { get; set; }
        public string register_email { get; set; }
        public string register_password { get; set; }
        /*
        protected override void OnInitialized()
        {

        }*/
        SqlConnection baglanti = new SqlConnection("workstation id=BlazorOSM.mssql.somee.com;packet size=4096;user id=mssql_58_SQLLogin_1;pwd=ztdvdv9bog;data source=BlazorOSM.mssql.somee.com;persist security info=False;initial catalog=BlazorOSM");

        public async void login_check()
        {
            try
            {
                baglanti.Open();
                SqlCommand cmd = new SqlCommand("Todo_user_check", baglanti);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@email", login_email);
                cmd.Parameters.AddWithValue("@password", login_password);
                SqlParameter outputParam = cmd.Parameters.Add("@result_rol", SqlDbType.Int);
                SqlParameter outputParam1 = cmd.Parameters.Add("@check_result", SqlDbType.Int);
                SqlParameter outputParam2 = cmd.Parameters.Add("@result_id", SqlDbType.Int);
                SqlParameter outputParam3 = cmd.Parameters.Add("@isim_", SqlDbType.NVarChar, 50);
                SqlParameter outputParam4 = cmd.Parameters.Add("@soyisim_", SqlDbType.NVarChar, 50);
                outputParam.Direction = ParameterDirection.Output;
                outputParam1.Direction = ParameterDirection.Output;
                outputParam2.Direction = ParameterDirection.Output;
                outputParam3.Direction = ParameterDirection.Output;
                outputParam4.Direction = ParameterDirection.Output;
                cmd.ExecuteScalar();
                if (outputParam.Value.ToString() == "1" && outputParam1.Value.ToString() == "1")
                {
                    navigationManager.NavigateTo("/main_page");
                    System.Console.WriteLine("Giriş Başarılı");
                    Login_info.email = login_email;
                    Login_info.password = login_password;
                    Login_info.id = Int32.Parse(outputParam2.Value.ToString());
                    Login_info.name = outputParam3.Value.ToString();
                    Login_info.surname = outputParam4.Value.ToString();

                }
                else if (outputParam.Value.ToString() == "2" && outputParam1.Value.ToString() == "1")
                {
                    navigationManager.NavigateTo("/ekip_lideri");
                    System.Console.WriteLine("Giriş Başarılı");
                    Login_info.email = login_email;
                    Login_info.password = login_password;
                    Login_info.id = Int32.Parse(outputParam2.Value.ToString());
                    Login_info.name = outputParam3.Value.ToString();
                    Login_info.surname = outputParam4.Value.ToString();
                }
                else if (outputParam.Value.ToString() == "3" && outputParam1.Value.ToString() == "1")
                {
                    navigationManager.NavigateTo("/normal_main");
                    System.Console.WriteLine("Giriş Başarılı");
                    Login_info.email = login_email;
                    Login_info.password = login_password;
                    Login_info.id = Int32.Parse(outputParam2.Value.ToString());
                    Login_info.name = outputParam3.Value.ToString();
                    Login_info.surname = outputParam4.Value.ToString();
            System.Console.WriteLine(outputParam3.Value.ToString());
                }
                else
                {
                    await jSRuntime.InvokeVoidAsync("login_error", login_email, login_password);
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message + " " + "sad");
            }
            finally
            {

                baglanti.Close();
            }

        }

        public void register()
        {
            try
            {
                baglanti.Open();
                SqlCommand cmd = new SqlCommand("Todo_user_register", baglanti);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@kullanici_Adi", register_name);
                cmd.Parameters.AddWithValue("@kullanici_soyadi", register_surname);
                cmd.Parameters.AddWithValue("@kullanici_email", register_email);
                cmd.Parameters.AddWithValue("@kullanici_password", register_password);
                SqlParameter outputParam = cmd.Parameters.Add("@result", SqlDbType.Int);
                outputParam.Direction = ParameterDirection.Output;
                cmd.ExecuteScalar();
                System.Console.WriteLine(outputParam.Value.ToString());
                if (outputParam.Value.ToString() == "1")
                    jSRuntime.InvokeVoidAsync("register_state", 1);
                else if (outputParam.Value.ToString() == "-1")
                    jSRuntime.InvokeVoidAsync("register_state", -1);

            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
            finally
            {
                baglanti.Close();
            }
        }
    }

}