using System;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Text;

using Microsoft.AspNetCore.Components;

namespace HyperCube.Pages
{
    public partial class Blockchains
    {
        Models.AccountModel _currentAccount;
        [Parameter]
        public int Smart { get; set; }
        public string answer;
        public string myWallet;
        public string balanceHCB
        {
            get { return HyperCube.Models.Blockchain.balanceHCB; }
            set { }
        }
        //public partial class Blocchains
        //{
        //    [Parameter]
        //    public string Smart { get; set; }
        //}

        //public string mypath = @"Wallet";
        public string mypath = @"getfile";

        public string getFiles(string wallet, string cmd, string pwd = "")
        {            
            mypath = @"c:\Users\Администратор\AppData\Local\Ethereum\rinkeby\keystore\";
            string ret = "";
            var files = Directory.GetFiles(mypath);
            foreach (var file in files)
            {
                string fileName = file.Substring(mypath.Length + 1);
                var name = fileName.Split("--");
                try
                {
                    if ("0x" + name[2] == wallet)
                    {
                        if (cmd == "key")
                        {
                            
                            Console.WriteLine($"pwd {pwd}");
                            return Models.Blockchain.ExecuteCommand(name[2] + " " + pwd)[1];
                        }
                        else if (cmd == "json")
                        {
                            var content = File.ReadAllText(file);
                            return content;
                        }
                    }                    
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Exception file {fileName}: {e.Message}");
                }                
            }            
            return ret;
        }
    }
}
