using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Sonex.DBWrapper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text.Json;
using System.Text.Json.Serialization;
using Windows.Foundation;
using Windows.Foundation.Collections;
using WinUI.TableView;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace Sonex.Pages;

/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class BlankPage2 : Page
{
    public BlankPage2()
    {
        try
        {
            InitializeComponent();

            Add.Click += TestBtnAsync_Click;
            Edit.Click += Edit_Click;
            DataGrid.HeaderHeight = 48;
            DataGrid.RowsHeight = 21;
            DataGrid.Loaded += DataGrid_Loaded;
        }catch(Exception e)
        {
            Debug.WriteLine(e.Message);
        }


    }

    private async void DataGrid_Loaded(object sender, RoutedEventArgs e)
    {
        DataGrid.LoadingRing = true;
        DataGrid.ItemsType = typeof(DapperTestTypesRow);
        DB.Result<DapperTestTypesRow>? r = await DB.QueryListAsync<DapperTestTypesRow>("select * from dapper_test_types LIMIT 100001");

        DataGrid.ItemsSource = r.Rows;
        DataGrid.LoadingRing = false;
    }

    private void Edit_Click(object sender, RoutedEventArgs e)
    {
        DataGrid.LoadingRing = !DataGrid.LoadingRing;
        //DataGrid.Pin("TextCol");
        //DataGrid.Pin("CharCol");
        //DataGrid.Pin("DateCol");
        //DataGrid.Pin("UuidCol");
    }

    private async void TestBtnAsync_Click(object sender, RoutedEventArgs e)
    {
        DataGrid.LoadingRing = true;
        var r = await DB.QueryListAsync<DapperTestTypesRow>("select * from dapper_test_types LIMIT 1000000");
        DataGrid.ItemsSource = r.Rows;
        DataGrid.LoadingRing = false;
        //var sw = Stopwatch.StartNew();


        //sw.Stop();


        // ResultText.Text = $"OK | Rows: {r.Count} | Time: {sw.ElapsedMilliseconds} ms";

    }

    private async void OnFilterApply(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs e)
    {
        DataGrid.SetFilter(e.QueryText);
        
    }
}


