package com.glxp.mipsdl.util;


import cn.hutool.core.util.StrUtil;
import com.glxp.mipsdl.entity.system.UdiEntity;
import com.glxp.mipsdl.util.gs1.AI;
import com.glxp.mipsdl.util.gs1.AIs;
import com.glxp.mipsdl.util.gs1.Gs1128Decoder;
import com.glxp.mipsdl.util.gs1.Gs1128Engine;

import java.util.Map;

public class FilterUdiUtils {
    private static final String TAG = "FilterUdiUtils";

    public static UdiEntity getUdi(String data) {
        if (data != null && data.length() > 2) {
            if (data.substring(0, 2).toUpperCase().equals("MA")) {
                return getZGCUdi(data);
            } else if (data.substring(0, 2).equals("01")) {
                return getGS1Udi(data);
            } else if (data.substring(0, 1).equals("#")) {
                return getGLXPUdi(data);
            }
        }
        return null;
    }


    public static UdiEntity getGS1Udi(String data) {
        Gs1128Engine engine = new Gs1128Engine();
        Gs1128Decoder decoder = engine.decoder();
        Map<AI, String> result = decoder.decode("]C1" + data);
        UdiEntity udiEntity = new UdiEntity();
        int length = 0;
        for (Map.Entry<AI, String> entry : result.entrySet()) {
            length = length + entry.getValue().length();
            if (entry.getKey() == AIs.GTIN) {
                udiEntity.setUdi(entry.getValue());
            } else if (entry.getKey() == AIs.BATCH_LOT) {
                udiEntity.setBatchNo(entry.getValue());
            } else if (entry.getKey() == AIs.EXPIRY) {
                udiEntity.setExpireDate(entry.getValue());
            } else if (entry.getKey() == AIs.PROD_DATE) {
                udiEntity.setProduceDate(entry.getValue());
            } else if (entry.getKey() == AIs.SERIAL) {
                udiEntity.setSerialNo(entry.getValue());
            }
        }
        if (data.length() - length > 22) {
            return null;
        }
        if (StrUtil.isEmpty(udiEntity.getUdi())) {
            return null;
        }
        if (udiEntity.getUdi().length() < 14
        ) {
            return null;
        } else
            return udiEntity;
    }

    public static UdiEntity getZGCUdi(String data) {
        String batchNo = "";
        String produceDate = "";
        String expireDate = "";
        String serialNo = null;
        String nameCode = "";
        String[] spilts = data.split("[.]");
        if (spilts != null && spilts.length >= 5) {
            for (int i = 0; i < 5; i++) {
                nameCode = nameCode + "." + spilts[i];
            }
            nameCode = nameCode.substring(1);
            for (int i = 0; i < spilts.length; i++) {
                String tempStr = spilts[i];
                if (tempStr != null && tempStr.length() > 1 && tempStr.substring(0, 1).equals("M")) {
                    produceDate = tempStr.substring(1);
                } else if (tempStr != null && tempStr.length() > 1 && tempStr.substring(0, 1).equals("L")) {
                    batchNo = tempStr.substring(1);
                } else if (tempStr != null && tempStr.length() > 1 && tempStr.substring(0, 1).equals("E")) {
                    expireDate = tempStr.substring(1);
                } else if (tempStr != null && tempStr.length() > 1 && tempStr.substring(0, 1).equals("S")) {
                    serialNo = tempStr.substring(1);
                }
            }
        }

        UdiEntity udiEntity = new UdiEntity();
        udiEntity.setBatchNo(batchNo);
        udiEntity.setExpireDate(expireDate);
        udiEntity.setProduceDate(produceDate);
        udiEntity.setUdi(nameCode);
        udiEntity.setSerialNo(serialNo);
        return udiEntity;

    }


    public static UdiEntity getGLXPUdi(String data) {
        String batchNo = "";
        String produceDate = "";
        String expireDate = "";
        String serialNo = null;
        String udi = "";
        String[] spilts = data.split("#", data.length());
        if (spilts != null && spilts.length >= 5) {
            udi = spilts[1];
            produceDate = spilts[2];
            expireDate = spilts[3];
            batchNo = spilts[4];
            if (spilts.length > 6) {
                serialNo = spilts[5];
            }

            if (StrUtil.isEmpty(batchNo)) {
                batchNo = null;
            }
            if (StrUtil.isEmpty(serialNo)) {
                serialNo = null;
            }

        }
        UdiEntity udiEntity = new UdiEntity();
        udiEntity.setBatchNo(batchNo);
        udiEntity.setExpireDate(expireDate);
        udiEntity.setProduceDate(produceDate);
        udiEntity.setUdi(udi);
        udiEntity.setSerialNo(serialNo);
        return udiEntity;
    }

    public static String getDiStr(String data) {
        String prefix = "";
        if (data != null && data.length() > 2) {
            if (data.substring(0, 2).equals("MA")) {
                String[] spilts = data.split("[.]");
                if (spilts != null && spilts.length >= 5) {
                    for (int i = 0; i < 5; i++) {
                        prefix = prefix + "." + spilts[i];
                    }
                    prefix = prefix.substring(1);
                }
            } else if (data.substring(0, 2).equals("01") && data.length() >= 16) {
                prefix = data.substring(2, 16);
            } else if (data.substring(0, 1).equals("#")) {
                String[] spilts = data.split("#");
                if (spilts != null && spilts.length >= 1)
                    prefix = spilts[1];
            }
        }
        if (prefix.equals(""))
            return data;
        return prefix;
    }


}
