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;
using System.Text.Json;
using Todo_App.Data;
using static Todo_App.Data.Hotmai_islemleri;
namespace Todo_App.Pages
{

    public partial class AdminTodosPage
    {

        public class gelen_ekipler
        {
            public int ekip_ID { get; set; }
            public string ekip_Adi { get; set; }
        }

        public class gelen_ekip_uyeleri
        {
            public int ID { get; set; }
            public string isim { get; set; }
            public string soyisim { get; set; }
        }
        SqlConnection baglanti3 = 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");

        [Inject]
        IJSRuntime JSRuntime { get; set; }

        public ElementReference edit { get; set; }

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                //  bool result= await JSRuntime.InvokeAsync<bool>("FroalaEditor",edit);

                var result = await JSRuntime.InvokeAsync<object>("eval", @"(function() { new FroalaEditor('#edit');  })()");

            }
        }
        List<gelen_ekipler> gelen_ekipler_list = new List<gelen_ekipler>();
        class Todo_Gorev_Getir
        {
            public int task_id { get; set; }
            public int task_team_id { get; set; }
            public string task_start { get; set; }
            public string task_finish { get; set; }
            public string task_content { get; set; }
            public int task_sender_id { get; set; }
            public int task_assigment_id { get; set; }
            public int task_id_2 { get; set; }
        }

        List<Todo_Gorev_Getir> todo_list_gorev = new List<Todo_Gorev_Getir>();

        class Todo_Gorev_Getir2
        {
            public int task_assigment_id { get; set; }
            public int task_id { get; set; }

        }

        List<Todo_Gorev_Getir2> todo_list_gorev2 = new List<Todo_Gorev_Getir2>();
        SqlConnection baglanti5 = 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");

        protected override void OnInitialized()
        {

            try
            {
                SqlCommand cmd = new SqlCommand("Todo_Gorev_Getir", baglanti5);
                cmd.CommandType = CommandType.StoredProcedure;
                baglanti5.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    todo_list_gorev.Add(new Todo_Gorev_Getir
                    {
                        task_id = Int32.Parse(dr["Task_ID"].ToString()),
                        task_team_id = Int32.Parse(dr["Task_Team_ID"].ToString()),
                        task_start = dr["Task_start_date"].ToString(),
                        task_finish = dr["Task_finish_date"].ToString(),
                        task_content = dr["Task_Content"].ToString(),
                        task_sender_id = Int32.Parse(dr["Task_sender_ID"].ToString())
                    });
                }
                baglanti5.Close();
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message + "a" + e.InnerException);
            }

            try
            {
                SqlCommand cmd = new SqlCommand("Todo_Gorev_Getir2", baglanti5);
                cmd.CommandType = CommandType.StoredProcedure;
                baglanti5.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    todo_list_gorev2.Add(new Todo_Gorev_Getir2
                    {
                        task_id = Int32.Parse(dr["Task_ID"].ToString()),
                        task_assigment_id = Int32.Parse(dr["Task_assigment_ID"].ToString())
                    });
                }
                baglanti5.Close();
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message + " " + e.InnerException);
            }



            try
            {
                SqlCommand cmd = new SqlCommand("Todo_Get_Teams", baglanti3);
                cmd.CommandType = CommandType.StoredProcedure;
                baglanti3.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    gelen_ekipler_list.Add(new gelen_ekipler { ekip_ID = Int32.Parse(dr["Team_ID"].ToString()), ekip_Adi = dr["Team_name"].ToString() });
                }
                baglanti3.Close();
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
        }
        List<KeyValuePair<int, string>> myList1 = new List<KeyValuePair<int, string>>();
        public int ekip_ID_listbox { get; set; }
        public async void ekip_uye_yukle(ChangeEventArgs e)
        {
            System.Console.WriteLine(e.Value.ToString());
            await JSRuntime.InvokeVoidAsync("removeOptions");
            await JSRuntime.InvokeVoidAsync("tarih_enabled", 4);

            ekip_ID_listbox = Int32.Parse(e.Value.ToString());
            List<gelen_ekip_uyeleri> gelen_ekipler_uye = new List<gelen_ekip_uyeleri>();

            try
            {
                SqlCommand cmd = new SqlCommand("Todo_Get_Users_2", baglanti3);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@takim_id", Int32.Parse(e.Value.ToString()));
                baglanti3.Open();
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    myList1.Add(new KeyValuePair<int, string>(Int32.Parse(dr["User_ID"].ToString()), dr["User_Email"].ToString()));
                    gelen_ekipler_uye.Add(new gelen_ekip_uyeleri { ID = Int32.Parse(dr["User_ID"].ToString()), isim = dr["User_Name"].ToString(), soyisim = dr["User_Surname"].ToString() });
                }
                baglanti3.Close();
                JSRuntime.InvokeVoidAsync("jsondeneme", gelen_ekipler_uye);

            }
            catch (Exception e1)
            {
                System.Console.WriteLine(e1.Message);
            }
        }

        public int kapsam_secim_id { get; set; }
        public void ekip_uye_data(ChangeEventArgs e)
        {
            JSRuntime.InvokeVoidAsync("tarih_enabled", 3);
            kapsam_secim_id = Int32.Parse(e.Value.ToString());
        }
        public string Baslangic_tarih { get; set; }
        public string Bitis_tarih { get; set; }
        public void tarih_getir(int a, ChangeEventArgs e)
        {
            switch (a)
            {
                case 1:
                    Baslangic_tarih = e.Value.ToString();
                    JSRuntime.InvokeVoidAsync("tarih_enabled", 1);
                    break;
                case 2:
                    Bitis_tarih = e.Value.ToString();
                    JSRuntime.InvokeVoidAsync("tarih_enabled", 2);
                    break;
            }
        }

        public string editMessage { get; set; }
        public string kullanici__email_foreach;

        public async void getEdit()
        {
            editMessage = await JSRuntime.InvokeAsync<string>("eval", @"(function() { return document.getElementById('edit').value;  })()", new object[] { edit });
            await InvokeAsync(StateHasChanged);

            switch (kapsam_secim_id)
            {
                case 1:
                    try
                    {
                        SqlCommand cmd = new SqlCommand("Todo_Get_Users_2", baglanti3);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@takim_id", ekip_ID_listbox);
                        baglanti3.Open();
                        SqlDataReader dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            Hotmai_islemleri.Microsoft(Login_info.name + " " + Login_info.surname, Login_info.email, "farcry58", dr["User_Email"].ToString(), "Yeni Gorev Bildirimi", editMessage.ToString(), null);

                            //        gelen_ekipler_uye.Add(new gelen_ekip_uyeleri { ID = Int32.Parse(dr["User_ID"].ToString()), isim = dr["User_Name"].ToString(), soyisim = dr["User_Surname"].ToString() });
                            deneme(1, Int32.Parse(dr["User_ID"].ToString()), Baslangic_tarih.ToString(), Bitis_tarih.ToString(), editMessage.ToString());
                        }
                        baglanti3.Close();

                    }
                    catch (Exception e)
                    {

                    }
                    break;
                default:
                    foreach (KeyValuePair<int, string> kvp in myList1)
                    {
                        if (kvp.Key == kapsam_secim_id)
                            kullanici__email_foreach = kvp.Value.ToString();
                    }
                    Hotmai_islemleri.Microsoft(Login_info.name + " " + Login_info.surname, Login_info.email, "farcry58", kullanici__email_foreach, "Yeni Gorev Bildirimi", editMessage.ToString(), null);
                    gorev_tekrar = 1;
                    deneme(1, kapsam_secim_id, Baslangic_tarih.ToString(), Bitis_tarih.ToString(), editMessage.ToString());
                    break;
            }
        }
        //       Hotmai_islemleri.Microsoft("Todo Sivas", "dvtksmk@hotmail.com", "farcry58", "Todo_deneme@hotmail.com", "Gorev Geldi", "Merhabalar Ekmek Alsana", null);
        SqlConnection baglanti4 = 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");
        int gorev_tekrar = 1;
        public async Task deneme(int par1, int par2, string par3, string par4, string par5)
        {
            SqlCommand cmd = new SqlCommand("Todo_Gorev_Kaydet", baglanti4);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@gorev_veren_id", Int32.Parse(par1.ToString()));
            cmd.Parameters.AddWithValue("@gorev_alan_id", Int32.Parse(par2.ToString()));
            cmd.Parameters.AddWithValue("@gorev_basla", par3);
            cmd.Parameters.AddWithValue("@gorev_bitis", par4);
            cmd.Parameters.AddWithValue("@gorev_icerik", par5);
            cmd.Parameters.AddWithValue("@gorev_tekrar", gorev_tekrar);
            baglanti4.Open();
            cmd.ExecuteNonQuery();
            baglanti4.Close();
            System.Console.WriteLine("merhalana");
            gorev_tekrar++;
        }
    }


}