using EVCB_OCPP.Domain;
using EVCB_OCPP.Packet.Features;
using EVCB_OCPP.Packet.Messages;
using EVCB_OCPP.Packet.Messages.Reservation;
using EVCB_OCPP.WSServer.Service.WsService;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using OCPPServer.Protocol;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace EVCB_OCPP.WSServer.Message
{
    internal partial class ProfileHandler
    {


        internal async Task<MessageResult> ExecuteReservationConfirm(Actions action, WsClientData session, IConfirmation confirm, string requestId)
        {
            MessageResult result = new MessageResult() { Success = true };

            switch (action)
            {
                case Actions.ReserveNow:
                    {
                        ReserveNowConfirmation _confirm = confirm as ReserveNowConfirmation;
                        ReserveNowRequest _request = _confirm.GetRequest() as ReserveNowRequest;
                        using (var db = await maindbContextFactory.CreateDbContextAsync())
                        {
                            var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
                            x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
                            if (operation != null)
                            {
                                operation.FinishedOn = DateTime.UtcNow;
                                operation.Status = 1;//電樁有回覆
                                operation.EVSE_Status = (int)_confirm.status;//OK
                                operation.EVSE_Value = _confirm.status.ToString();
                                await db.SaveChangesAsync();
                            }

                        }
                    }
                    break;
                case Actions.CancelReservation:
                    {
                        CancelReservationConfirmation _confirm = confirm as CancelReservationConfirmation;
                        CancelReservationRequest _request = _confirm.GetRequest() as CancelReservationRequest;
                        using (var db = await maindbContextFactory.CreateDbContextAsync())
                        {
                            var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
                            x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
                            if (operation != null)
                            {
                                operation.FinishedOn = DateTime.UtcNow;
                                operation.Status = 1;//電樁有回覆
                                operation.EVSE_Status = (int)_confirm.status;//OK
                                operation.EVSE_Value = _confirm.status.ToString();
                                await db.SaveChangesAsync();
                            }

                        }
                    }
                    break;
                default:
                    {
                        logger.LogWarning(string.Format("Not Implement {0} Logic", confirm.GetType().ToString().Replace("OCPPPackage.Messages.RemoteTrigger.", "")));
                    }
                    break;
            }
            return result;
        }


        internal async Task<MessageResult> ExecuteReservationError(Actions action, string errorMsg, WsClientData session, string requestId)
        {
            MessageResult result = new MessageResult() { Success = true };

            switch (action)
            {
                case Actions.ReserveNow:
                    {
                        using (var db = await maindbContextFactory.CreateDbContextAsync())
                        {
                            var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
                            x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
                            if (operation != null)
                            {
                                operation.FinishedOn = DateTime.UtcNow;
                                operation.Status = 1;//電樁有回覆
                                operation.EVSE_Status = (int)255;//錯誤
                                operation.EVSE_Value = errorMsg;
                                await db.SaveChangesAsync();
                            }

                        }
                    }
                    break;
                case Actions.CancelReservation:
                    {
                        using (var db = await maindbContextFactory.CreateDbContextAsync())
                        {
                            var operation = await db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
                            x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefaultAsync();
                            if (operation != null)
                            {
                                operation.FinishedOn = DateTime.UtcNow;
                                operation.Status = 1;//電樁有回覆
                                operation.EVSE_Status = (int)255;//錯誤
                                operation.EVSE_Value = errorMsg;
                                await db.SaveChangesAsync();
                            }

                        }
                    }
                    break;


                default:
                    {
                        logger.LogWarning(string.Format("Not Implement {0} Logic", action));
                    }
                    break;
            }
            return result;

        }
    }
}