package com.neptune.plugin.models;

import com.pax.dal.entity.ECheckMode;
import com.pax.dal.entity.EPedKeyType;

import org.json.JSONException;
import org.json.JSONObject;

import com.neptune.utils.Convert;
import com.neptune.utils.Convert.EPaddingPosition;

public class KeyModel {
    private EPedKeyType srcKeyType;
    private byte srcKeyIndex;
    private EPedKeyType destKeyType;
    private byte destkeyIndex;
    private byte[] destKeyValue;
    private ECheckMode checkMode;
    private byte[] checkBufx;

    public KeyModel(EPedKeyType srcKeyType, byte srcKeyIndex, EPedKeyType destKeyType, byte destkeyIndex, byte[] destKeyValue, ECheckMode checkMode, byte[] checkBufx) {
        this.srcKeyType = srcKeyType;
        this.srcKeyIndex = srcKeyIndex;
        this.destKeyType = destKeyType;
        this.destkeyIndex = destkeyIndex;
        this.destKeyValue = destKeyValue;
        this.checkMode = checkMode;
        this.checkBufx = checkBufx;
    }

    public KeyModel(JSONObject jsonObject) throws JSONException {
        this(
                ePedKeyTypeToEnum(jsonObject.getString("srcKeyType")),
                (byte) jsonObject.getInt("srcKeyIndex"),
                ePedKeyTypeToEnum(jsonObject.getString("destKeyType")),
                (byte) jsonObject.getInt("destkeyIndex"),
                Convert.getInstance().strToBcd(jsonObject.getString("destKeyValue"), EPaddingPosition.PADDING_LEFT),
                eCheckModeToEnum(jsonObject.getString("checkMode")),
                Convert.getInstance().strToBcd(jsonObject.getString("checkBufx"), EPaddingPosition.PADDING_LEFT)
        );

    }

    private static EPedKeyType ePedKeyTypeToEnum(String pedType){
        for (EPedKeyType type : EpedKeyTypeValues()) {
            if (type.name().equals(pedType)) {
                return type;
            }
        }
        return null;
    }
    public static EPedKeyType[] EpedKeyTypeValues(){
        return new EPedKeyType[]{
                EPedKeyType.TLK,
                EPedKeyType.TMK,
                EPedKeyType.TPK,
                EPedKeyType.TAK,
                EPedKeyType.TDK,
                EPedKeyType.TIK,
                EPedKeyType.SM2_PUB_KEY,
                EPedKeyType.SM2_PVT_KEY,
                EPedKeyType.SM4_TAK,
                EPedKeyType.SM4_TDK,
                EPedKeyType.SM4_TMK,
                EPedKeyType.SM4_TPK,
                EPedKeyType.TAESK
        };
    }



    private static ECheckMode eCheckModeToEnum(String pedType){
        for (ECheckMode type : eCheckModeValues()) {
            if (type.name().equals(pedType)) {
                return type;
            }
        }
        return null;
    }
    public static ECheckMode[] eCheckModeValues(){
        return new ECheckMode[]{
                ECheckMode.KCV_NONE,
                ECheckMode.KCV_ENCRYPT_0,
                ECheckMode.KCV_ENCRYPT_FIX_DATA,
                ECheckMode.KCV_MAC_INPUT_DATA,
                ECheckMode.KCV_SM4_ENCRYPT_0
        };
    }


    public EPedKeyType getSrcKeyType() {
        return srcKeyType;
    }

    public byte getSrcKeyIndex() {
        return srcKeyIndex;
    }

    public EPedKeyType getDestKeyType() {
        return destKeyType;
    }

    public byte getDestkeyIndex() {
        return destkeyIndex;
    }

    public byte[] getDestKeyValue() {
        return destKeyValue;
    }

    public ECheckMode getCheckMode() {
        return checkMode;
    }

    public byte[] getCheckBufx() {
        return checkBufx;
    }

}
