using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components;
using System.Diagnostics;
using System.Globalization;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using Microsoft.JSInterop;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Net.Http;
using Microsoft.Data.SqlClient;


using System.Data;

namespace ex9.Pages.Moderator
{
    public class PersonModel
    {
        public string? Id { get; set; }
        public string? Name { get; set; }
        public string? Url { get; set; }
        public string? Yazar { get; set; }


    }
    public class Pr81Base : ComponentBase
    {
        static string connectionString = ("Server=.;Database=PdfApp;Encrypt=False;Integrated Security=SSPI;");

        static SqlConnection conn = new SqlConnection(connectionString);
        static string sql = "select Id,Name,Url,Yazar from Pdf";
        static SqlDataAdapter daps = new SqlDataAdapter(sql, conn);
        SqlCommandBuilder cb = new SqlCommandBuilder(daps);
        DataSet dsps = new DataSet();
        public List<PersonModel> people = new List<PersonModel>();
        public string? error { get; set; }
        public string? inputId { get; set; }
        public string? inputName { get; set; }
        public string? inputUrl { get; set; }
        public string? inputYazar { get; set; }

        private string? kontrol = "";
        [Inject]
        public ProtectedLocalStorage? localstr { get; set; }
        public string? Kontrol
        {
            get
            {
                AdminModerator();
                return kontrol;
            }
            set
            {
                kontrol = value;
            }
        }
        public string? mystr { get; set; }
        [Inject]
        protected NavigationManager? Navigation { get; set; }
       
        private async void AdminModerator()
        {
            var result = await localstr.GetAsync<string>("myUser");
            mystr = result.Success ? result.Value : "";
            if (mystr != "admin")
                if (mystr != "moderator")
                    Navigation.NavigateTo("/");
            mystr = "";
        }

        public async Task InsertData()
        {
            dsps.Tables[0].Rows.Add(null, inputName, inputUrl, inputYazar);
            daps.Update(dsps, "people");
            dsps.Tables["people"].Clear();
            //cb.Dispose();
            inputName = "";
            inputUrl = "";
            inputYazar = "";
            await OnInitializedAsync();
        }
        public async Task GetData()
        {
            DataRowCollection itemColumns = dsps.Tables[0].Rows;
            for (int i = 0; i < dsps.Tables[0].Rows.Count; i++)
            {
                if (dsps.Tables[0].Rows[i]["Id"].ToString() == inputId)
                {
                    inputName = dsps.Tables[0].Rows[i]["Name"].ToString();
                    inputUrl = dsps.Tables[0].Rows[i]["Url"].ToString();
                    inputYazar = dsps.Tables[0].Rows[i]["Yazar"].ToString();
                }
            }
            dsps.Tables[0].Clear();
            await OnInitializedAsync();
        }
        public async Task UpdateData()
        {
            DataRowCollection itemColumns = dsps.Tables[0].Rows;
            for (int i = 0; i < dsps.Tables[0].Rows.Count; i++)
            {
                if (dsps.Tables[0].Rows[i]["Id"].ToString() == inputId)
                {
                    dsps.Tables[0].Rows[i]["Name"] = inputName;
                    dsps.Tables[0].Rows[i]["Url"] = inputUrl;
                    dsps.Tables[0].Rows[i]["Yazar"] = inputYazar;
                }
            }
            daps.Update(dsps, "people");
            // cb.Dispose();
            dsps.Tables[0].Clear();

            inputId = "";
            inputName = "";
            inputUrl = "";
            inputYazar = "";
            await OnInitializedAsync();
        }
        public async Task DeleteData()
        {
            foreach (DataRow row in dsps.Tables[0].Rows)
            {
                if (row["Id"].ToString() == inputId)

                    row.Delete();
            }
            daps.Update(dsps, "people");
            //  cb.Dispose();
            dsps.Tables["people"].Clear();

            inputId = "";
            inputName = "";
            await OnInitializedAsync();

        }
        protected override async Task OnInitializedAsync()
        {
            people.Clear();
            await selectproc();
        }
        public Task selectproc()
        {
            return Task.Run(() =>
            {
                try
                {
                    daps.Fill(dsps, "people");
                    error += dsps.Tables[0].Rows.Count.ToString();
                    for (int i = 0; i < dsps.Tables[0].Rows.Count; i++)
                    {
                        PersonModel pm = new PersonModel();
                        pm.Id = dsps.Tables[0].Rows[i]["Id"].ToString();
                        pm.Name = dsps.Tables[0].Rows[i]["Name"].ToString();
                        pm.Url = dsps.Tables[0].Rows[i]["Url"].ToString();
                        pm.Yazar = dsps.Tables[0].Rows[i]["Yazar"].ToString();

                        people.Add(pm);

                    }
                }
                catch (Exception ex)
                {
                    error = ex.ToString();
                }
            });
        }

    }
}