using Communicator.HttpServerWebApiCommunicator;
using DeviceProcessor;
using Newtonsoft.Json;
using Parser.HttpMessageParser;
using Sinochem_InternetPlus_Display.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.Results;
using Wayne.FDCPOSLibrary;

namespace Sinochem_InternetPlus_Display
{
    public class Handler : IHandler<string, BaseHttpMessage<string>>, IFdcCommunicableController
    {
        public Guid Id => new Guid();

        public Func<string, bool> BroadcastMessageViaFdc { get; set; }
        public Func<string, Tuple<string, OverallResult>> OnMessageReceivedViaFdc { get; set; }

        private int bindingPumpId;
        public Handler(int bindingPumpId)
        {
            this.bindingPumpId = bindingPumpId;
            this.OnMessageReceivedViaFdc += (msg) =>
            {
                Console.WriteLine("I received sth via FDC protocol: " + msg);
                return new Tuple<string, OverallResult>("thanks for you message", OverallResult.Success);
            };
        }
        public void Init(IContext<string, BaseHttpMessage<string>> context)
        {
            var pump = DeviceControllerBus.Default.OfType<IFdcPumpController>().FirstOrDefault(p => p.PumpId == this.bindingPumpId);
            if (pump != null)
                pump.OnCurrentFuellingStatusChange += Handler_OnCurrentFuellingStatusChange;
        }

        public void Process(IContext<string, BaseHttpMessage<string>> context)
        {
            var httpRequest = context.Incoming.Message;
            Console.WriteLine("I read car plate request: " + httpRequest.Content);
            //DeviceControllerBus.Default.OfType<IFdcPumpController>().First(p => p.PumpId == 1).Authorize(1);

            CarPlateTrxRequest carPlateTrxRequest = JsonConvert.DeserializeObject<CarPlateTrxRequest>(httpRequest.Content);
            Console.WriteLine(carPlateTrxRequest.car_Number);

            CarPlateTrxResponse carPlateTrxResponse = new CarPlateTrxResponse
            {
                code = "200",
                message = "this is from Sinochem_CarPlateRecognizeCamera_HuLianWangJia"
            };

            var httpResponse = SimpleHttpOutgoingMessage.CreatedFrom(httpRequest,
                new OkNegotiatedContentResult<CarPlateTrxResponse>(carPlateTrxResponse, context.Incoming.Message.ApiController));
            context.Outgoing.Write(httpResponse);

            // send msg to 15` to show 'pls start fuelling'
            var isSendSucceed = this.BroadcastMessageViaFdc?.Invoke("pls start fuelling!");
        }

        private void Handler_OnCurrentFuellingStatusChange(object sender, FdcTransactionDoneEventArg e)
        {
            // wait for fuelling done
            if (e.Transaction.Finished)
            {
                // send msg to 15` to show 'fuelling is done'
                var chargeAmount = e.Transaction.Amount;
                // send msg to cloud to charge.
            }
            else
            {
                var runningAmount = e.Transaction.Amount;
                // send msg to 15` to show 'fuelling is in progress'
            }
        }
    }
}