using iq2dev.com.IQCommLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iq2dev.com.iqCommLib;

public enum IQGadgetID
{
    ReservedID,
    IQInterface,
    IQLink
}

public enum MST_IsduCacheModeE
{
    MST_ISDU_CACHE_DISABLED = 0x00,
    MST_ISDU_CACHE_ENABLE,
    MST_FAST_ISDU_PROCESSING_ENABLED
};

public enum MST_IsduCacheModeErrorHandlingE
{
    MST_ISDU_CACHE_REPORT_ERROR_IMMIDIATELY = 0x00,
    MST_ISDU_CACHE_REPORT_ERROR_AT_THE_END
}

public struct IsduCachedInfo
{
    public MST_IsduCacheModeE CachedMode;
    public MST_IsduCacheModeErrorHandlingE ErrorHandlingMode;
    public bool IsIsduInTransfer;

    public IsduCachedInfo(List<byte> data)
    {
        if (data.Count < 3)
        {
            throw new InvalidOperationException($"Invalid data size for parameter IQParSetIsduCashedMode read: {data.Count}, but should be >= 3");
        }

        CachedMode = (MST_IsduCacheModeE)data[0];
        ErrorHandlingMode = (MST_IsduCacheModeErrorHandlingE)data[1];
        IsIsduInTransfer = data[2] == 1 ? true : false;
    }
}

public interface IProtocolCommunicator
{
    #region Send / receive raw data
    public List<byte> SendRecvCommand(IQCommChannel channel, IQCommand command, List<byte> transmitData);

    public List<byte> SendRecvCommand(IQCommChannel channel, byte command, List<byte> transmitData);

    #endregion Send / receive raw data

    #region IQ Interface

    public IQGadgetID ReadID();
    public uint GetMasterCycleTime();
    public uint GetODLengthOperate();
    public void writeIQCommPar(IQPar number, List<byte> data);
    public List<byte> readIQCommPar(IQPar number);
    public void writeDirectMode(List<byte> data);
    public List<byte> readDirectMode();
    public IsduCachedInfo ReadIsduCachedInfo();

    #endregion

    #region Master

    public IQMstStatus getMstStatus(List<byte> pdIn);

    public void setPDOut(List<byte> pdOut);

    public void setOperMode(IQMstMode mode, int timeoutSeconds);

    public void setOperMode(IQMstMode mode);

    public void startODWrite(UInt16 index, byte subIndex, List<byte> data);

    public List<byte> startODWriteCashed(UInt16 index, byte subIndex, List<byte> data);

    public void getODWriteResponse();

    public void startODRead(UInt16 index, byte subIndex);

    public List<byte> getODReadResponse();

    public IOLEvent readEvent();

    public List<byte> ReadOD(UInt16 index, byte subIndex);

    public void WriteOD(UInt16 index, byte subIndex, List<byte> data);

    public List<byte> WriteODCashed(UInt16 index, byte subIndex, List<byte> data);

    public void WaitODRsp();

    #endregion

    #region Device

    public IQDevStatus getDevStatus(List<byte> pdOut);

    public void setPDIn(bool IsInvalid, List<byte> pdIn);

    public IQODRequest getODRequest();

    public void sendEvent(IOLEvent Event);

    public void addISDU(List<byte> isdu);

    public List<byte> GetISDU(UInt16 address);
    public void SetISDUValue(UInt16 index, byte subindex, List<byte> value);

    public List<byte> GetISDUValue(UInt16 index, byte subindex);

    public void removeAllISDU();

    #endregion
}
