using EVCB_OCPP.Domain;
using EVCB_OCPP.Packet.Features;
using EVCB_OCPP.Packet.Messages;
using EVCB_OCPP.Packet.Messages.LocalAuthListManagement;
using OCPPServer.Protocol;
using System;
using System.Linq;

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




        internal MessageResult ExecuteLocalAuthListManagementConfirm(Actions action, ClientData session, IConfirmation confirm, string requestId)
        {
            MessageResult result = new MessageResult() { Success = true };

            switch (action)
            {
                case Actions.GetLocalListVersion:
                    {
                        GetLocalListVersionConfirmation _confirm = confirm as GetLocalListVersionConfirmation;
                        GetLocalListVersionRequest _request = _confirm.GetRequest() as GetLocalListVersionRequest;
                        using (var db = new MainDBContext())
                        {
                            var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
                            x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
                            if (operation != null)
                            {
                                operation.FinishedOn = DateTime.UtcNow;
                                operation.Status = 1;//電樁有回覆
                                operation.EVSE_Status = 1;//OK
                                operation.EVSE_Value = _confirm.listVersion.ToString();
                                db.SaveChanges();
                            }

                        }
                    }
                    break;
                case Actions.SendLocalList:
                    {
                        SendLocalListConfirmation _confirm = confirm as SendLocalListConfirmation;
                        SendLocalListRequest _request = _confirm.GetRequest() as SendLocalListRequest;
                        using (var db = new MainDBContext())
                        {
                            var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
                            x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
                            if (operation != null)
                            {
                                operation.FinishedOn = DateTime.UtcNow;
                                operation.Status = 1;//電樁有回覆
                                operation.EVSE_Status = (int)_confirm.status;//OK     
                                operation.EVSE_Value = _confirm.status.ToString();
                                db.SaveChanges();
                            }

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


        internal MessageResult ReceivedLocalAuthListManagementError(Actions action, string errorMsg, ClientData session, string requestId)
        {
            MessageResult result = new MessageResult() { Success = true };

            switch (action)
            {
                case Actions.SendLocalList:
                case Actions.GetLocalListVersion:
                    {
                        using (var db = new MainDBContext())
                        {
                            var operation = db.MachineOperateRecord.Where(x => x.SerialNo == requestId &&
                            x.ChargeBoxId == session.ChargeBoxId && x.Status == 0).FirstOrDefault();
                            if (operation != null)
                            {
                                operation.FinishedOn = DateTime.UtcNow;
                                operation.Status = 1;//電樁有回覆
                                operation.EVSE_Status = (int)255;//錯誤
                                operation.EVSE_Value = errorMsg;
                                db.SaveChanges();
                            }

                        }
                    }
                    break;

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

        }
    }
}
