﻿using System;
using System.Collections.Generic;
using MinerControl.PriceEntries;
using MinerControl.Utility;

namespace MinerControl.Services
{
    public class LtcRabbitService : ServiceBase<LtcRabbitPriceEntry>
    {
        // https://www.ltcrabbit.com/index.php?page=api&action=public
        // {"hashrate":2394110,"hashrate_scrypt":2394110,"hashrate_x11":3971072,"workers":1765,"ltc_mh_scrypt":"0.0257857","ltc_mh_x11":"0.0328172",
        //  "profitability":
        //    {"current":{
        //        "scrypt":{"ltc_mh":"0.0257857","btc_mh":"0.0002651","vs_ltc":"93.60"},
        //        "x11":{"ltc_mh":"0.0328172","btc_mh":"0.0003374","vs_ltc":"476.49"}},
        //     "history":{
        //       "scrypt":[{"date":"2014-10-20","ltc_mh":"0.0171152","btc_mh":"0.0001757","vs_ltc":"62.08"},<snip>],
        //       "x11":[{"date":"2014-10-20","ltc_mh":"0.0303198","btc_mh":"0.0003113","vs_ltc":"439.88"},<snip>]}
        //  }}

        // https://www.ltcrabbit.com/index.php?page=api&action=getappdata&appname=general&appversion=1&api_key=<apikey>
        // {"getappdata":{
        //    "general":{"message":""},
        //    "pool":{"hashrate_scrypt":2271520,"hashrate_x11":3859754,"workers":1783,"ltc_mh_scrypt":"0.0254868","ltc_mh_x11":"0.0323970","hashrate":2271520},
        //    "profitability": <same as public api above>,
        //    "ltc_exchange_rates":{"USD":"3.88","EUR":"3.104"},
        //    "btc_exchange_rates":{"USD":"379.736","EUR":"301.00000"},
        //    "user":{"username":"MinerControl","balance":0,"balance_btc":"0.0000033","hashrate_scrypt":0,"hashrate_x11":0,"invalid_shares_scrypt":0,"invalid_shares_x11":0,"sharerate":0,"invalid_share_rate":0,"hashrate":0},
        //    "worker":[],
        //    "earnings":{"basis":[],"alt":[],"24h_total":0,"24h_basis":0,"24h_alt":0,"24h_affiliate":0,"48h_total":0,"48h_basis":0,"48h_alt":0,"48h_affiliate":0}}}

        private string _apikey;

        public LtcRabbitService()
        {
            ServiceName = "LTCRabbit";
            DonationAccount = "MinerControl";
            DonationWorker = "1";
        }

        public override void Initialize(IDictionary<string, object> data)
        {
            ExtractCommon(data);
            _apikey = data.GetString("apikey");

            object[] items = data["algos"] as object[];
            foreach (object rawitem in items)
            {
                Dictionary<string, object> item = rawitem as Dictionary<string, object>;
                LtcRabbitPriceEntry entry = CreateEntry(item);

                Add(entry);
            }
        }

        public override void CheckPrices()
        {
            ClearStalePrices();
            WebUtil.DownloadJson("https://www.ltcrabbit.com/index.php?page=api&action=public", ProcessPrices);
            WebUtil.DownloadJson(
                string.Format(
                    "https://www.ltcrabbit.com/index.php?page=api&action=getappdata&appname=MinerControl&appversion=1&api_key={0}",
                    _apikey), ProcessBalances);
        }

        private void ProcessPrices(object jsonData)
        {
            Dictionary<string, object> data = jsonData as Dictionary<string, object>;
            Dictionary<string, object> profitability = data["profitability"] as Dictionary<string, object>;
            Dictionary<string, object> current = profitability["current"] as Dictionary<string, object>;

            lock (MiningEngine)
            {
                foreach (string key in current.Keys)
                {
                    object rawitem = current[key];
                    Dictionary<string, object> item = rawitem as Dictionary<string, object>;
                    string algo = key.ToLower();

                    LtcRabbitPriceEntry entry = GetEntry(algo);
                    if (entry == null) continue;

                    entry.Price = item["btc_mh"].ExtractDecimal()*1000;
                }

                MiningEngine.PricesUpdated = true;
                MiningEngine.HasPrices = true;

                LastUpdated = DateTime.Now;

                UpdateHistory();
            }
        }

        private void ProcessBalances(object jsonData)
        {
            Dictionary<string, object> data = jsonData as Dictionary<string, object>;
            Dictionary<string, object> getappdata = data["getappdata"] as Dictionary<string, object>;
            Dictionary<string, object> ltc_exchange_rates = getappdata["ltc_exchange_rates"] as Dictionary<string, object>;
            Dictionary<string, object> btc_exchange_rates = getappdata["btc_exchange_rates"] as Dictionary<string, object>;
            Dictionary<string, object> user = getappdata["user"] as Dictionary<string, object>;
            Balance = user["balance_btc"].ExtractDecimal();

            LtcRabbitPriceEntry entry = GetEntry("x11");
            if (entry != null)
            {
                decimal hashrate = user["hashrate_x11"].ExtractDecimal();
                entry.AcceptSpeed = hashrate/1000;
            }

            entry = GetEntry("scrypt");
            if (entry != null)
            {
                decimal hashrate = user["hashrate_scrypt"].ExtractDecimal();
                entry.AcceptSpeed = hashrate/1000;
            }
        }
    }
}