using System.Collections.Generic;
using NcProgramManager;
using NcProgramManager.Cnc.Models;

namespace NcProgramManager.Cnc.Fanuc
{
    internal static class FanucErrorTranslator
    {
        private static readonly Dictionary<short, string> Messages = new Dictionary<short, string>
        {
            { (short)Focas1.focas_ret.EW_PROTOCOL, "protocol error" },
            { (short)Focas1.focas_ret.EW_SOCKET,   "windows socket error" },
            { (short)Focas1.focas_ret.EW_NODLL,    "DLL not exist error" },
            { (short)Focas1.focas_ret.EW_BUS,      "bus error" },
            { (short)Focas1.focas_ret.EW_SYSTEM2,  "system error" },
            { (short)Focas1.focas_ret.EW_HSSB,     "hssb communication error" },
            { (short)Focas1.focas_ret.EW_HANDLE,   "Windows library handle error" },
            { (short)Focas1.focas_ret.EW_VERSION,  "CNC/PMC version mismatch" },
            { (short)Focas1.focas_ret.EW_UNEXP,    "abnormal error" },
            { (short)Focas1.focas_ret.EW_SYSTEM,   "system error" },
            { (short)Focas1.focas_ret.EW_PARITY,   "shared RAM parity error" },
            { (short)Focas1.focas_ret.EW_MMCSYS,   "emm386 or mmcsys install error" },
            { (short)Focas1.focas_ret.EW_RESET,    "reset or stop occurred" },
            { (short)Focas1.focas_ret.EW_BUSY,     "busy" },
            { (short)Focas1.focas_ret.EW_FUNC,     "command prepare error / pmc not exist" },
            { (short)Focas1.focas_ret.EW_LENGTH,   "data block length error" },
            { (short)Focas1.focas_ret.EW_NUMBER,   "data number / address range error" },
            { (short)Focas1.focas_ret.EW_ATTRIB,   "data attribute / data type error" },
            { (short)Focas1.focas_ret.EW_DATA,     "data error" },
            { (short)Focas1.focas_ret.EW_NOOPT,    "no option" },
            { (short)Focas1.focas_ret.EW_PROT,     "write protect" },
            { (short)Focas1.focas_ret.EW_OVRFLOW,  "memory overflow" },
            { (short)Focas1.focas_ret.EW_PARAM,    "CNC parameter not correct" },
            { (short)Focas1.focas_ret.EW_BUFFER,   "buffer empty/full" },
            { (short)Focas1.focas_ret.EW_PATH,     "path error" },
            { (short)Focas1.focas_ret.EW_MODE,     "CNC mode error" },
            { (short)Focas1.focas_ret.EW_REJECT,   "execution rejected" },
            { (short)Focas1.focas_ret.EW_DTSRVR,   "data server error" },
            { (short)Focas1.focas_ret.EW_ALARM,    "alarm has been occurred" },
            { (short)Focas1.focas_ret.EW_STOP,     "CNC is not running" },
            { (short)Focas1.focas_ret.EW_PASSWD,   "protection data error" }
        };

        public static CncError Translate(short code, string source)
        {
            string msg;
            if (!Messages.TryGetValue(code, out msg))
                msg = "unknown FOCAS error";
            return new CncError(code, source, msg);
        }

        public static bool IsDisconnectError(short code)
        {
            return code == (short)Focas1.focas_ret.EW_SOCKET
                || code == (short)Focas1.focas_ret.EW_HANDLE
                || code == (short)Focas1.focas_ret.EW_HSSB
                || code == (short)Focas1.focas_ret.EW_PROTOCOL;
        }
    }
}
