
package org.ts.ddcs.dataStream;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.ts.ddcs.constant.BusinessConstant;
import org.ts.ddcs.datatemplate.TestState;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 数据缓存
 *
 * @author swp
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class DataStreamCache {
    private TestState state = TestState.LOOK;
    private byte[] dataBuff;
    private int readSize;
    private String datagramCode;
    private String rtuCode;
    private LocalDateTime pickPacketTime;
    private Integer datagramSize;
    private byte[] datagramBuff;
    private DataStreamProcessor matchProcessor;

    private Map<String, Object> keyStore = new HashMap<>();

    public void resetStore() {
        this.keyStore.clear();
    }

    public int remainDataSize() {
        if (null != this.dataBuff) {
            if (this.readSize == this.dataBuff.length) {
                return 0;
            }
            return this.dataBuff.length - this.readSize;
        }
        return 0;
    }

    public void readIndex(int readSize) {
        this.readSize = readSize;
    }

    public void readSize(int readSize) {
        this.readSize = this.readSize + readSize;
    }


    public int dataSize() {
        if (null != dataBuff) {
            return dataBuff.length;
        }
        return 0;
    }

    public byte getByte() throws NullPointerException, IndexOutOfBoundsException {
        return this.dataBuff[this.readSize];
    }

    public byte[] getBytes(int length) throws NullPointerException, IndexOutOfBoundsException {
        byte[] buf = new byte[length];
        System.arraycopy(this.dataBuff, this.readSize, buf, 0, length);
        return buf;
    }

    public byte getByte(int pos) throws NullPointerException, IndexOutOfBoundsException {
        return this.dataBuff[pos];
    }

    public byte[] getBytes(int pos, int length) throws NullPointerException, IndexOutOfBoundsException {
        byte[] buf = new byte[length];
        System.arraycopy(this.dataBuff, pos, buf, 0, length);
        return buf;
    }

    public void putValue(String key, Object value) {
        this.keyStore.put(key, value);
    }

    public Set<String> keys() {
        return this.keyStore.keySet();
    }

    public Object getValue(String key) {
        return this.keyStore.get(key);
    }

    public Integer getIntValue(String key) {
        Object value = this.keyStore.get(key);
        if (value instanceof Integer) {
            return (Integer) value;
        }
        return null;
    }

    public byte[] getBytesValue(String key) {
        Object value = this.keyStore.get(key);
        if (value instanceof byte[]) {
            return (byte[]) value;
        }
        return null;
    }

    public String getStringValue(String key) {
        Object value = this.keyStore.get(key);
        if (value instanceof String) {
            return (String) value;
        }
        return null;
    }

    public Object getItemValue(String key) {
        Object itemTemplateCacheObj = this.keyStore.get(BusinessConstant.SW_DATA_STREAM_CACHE_VALUE);
        if (null != itemTemplateCacheObj) {
            if (itemTemplateCacheObj instanceof DataItemTemplateCache) {
                DataItemTemplateCache itemTemplateCache = (DataItemTemplateCache) itemTemplateCacheObj;
                return itemTemplateCache.getValue(key);
            }
        }
        return null;
    }
}
