using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.JSInterop;

namespace AbrBlazorTools
{
    public class BrowserTools : IBrowserTools
    {
        private readonly IJSRuntime _js;

        public BrowserTools(IJSRuntime js)
        {
            _js = js;
        }

        public async Task<BrowserDimension> GetDimensions()
        {
            return await _js.InvokeAsync<BrowserDimension>("methods.getDimensions");
        }
        public async Task<ValueTask> CopyText(string text)
        {
            return _js.InvokeVoidAsync("navigator.clipboard.writeText", text);
        }

        public async Task PrepareResponsiveTable()
        {
            await _js.InvokeVoidAsync("methods.PrepareResponsiveTable");
            
        }

        public async Task<string> GetInputValueById(string id)
        {
            return await _js.InvokeAsync<string>("AbrMethods.getInputValue",id);
        }

        public async Task<string> GetInputValueByIdAndReplacePersianNumbers(string id)
        {
            var text = await GetInputValueById(id);
            var digits = new string[] { "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹" };
            var english = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            for (var i = 0; i < digits.Length; i++)
                text = text.Replace(digits[i], english[i]);
            return text;
        }

        public async Task ScrollToEnd()
        {
            await _js.InvokeVoidAsync("methods.ScrollToEnd");

        }
    }

    public class BrowserDimension
    {
        public int Width { get; set; }
        public int Height { get; set; }
    }
    public interface IBrowserTools
    {
        Task<BrowserDimension> GetDimensions();
        Task<ValueTask> CopyText(string text);
        Task PrepareResponsiveTable();
        Task<string> GetInputValueById(string id);
        Task<string> GetInputValueByIdAndReplacePersianNumbers(string id);
        Task ScrollToEnd();
    }
    }
