package beast.evolution.datatype;


import beast.core.Description;
import beast.core.Input;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Description("Integer data type to describe discrete morphological characters with polymorphisms")
public class StandardData extends DataType.Base {

    public Input<Integer> maxNrOrStatesInput = new Input<Integer>("nrOfStates", "specifies the maximum number of " +
            "character states in data matrix or in the filtered alignment", Input.Validate.REQUIRED);
    public Input<String> listOfAmbiguitiesInput = new Input<String>("ambiguities", "all possible ambiguities presented " +
            "as space separated sets of ordered elements. Elements are digits 0..9.");
    public Input<List<CharStateLabels>> charStateLabelsInput= new Input<List<CharStateLabels>>("charstatelabels",
            "list of morphological character descriptions. Position in the list corresponds to the position of the" +
                    "character in the alignment");

    private String[] ambiguities;
    private ArrayList<String> codeMapping;

    private int ambCount;


    public StandardData() {
        stateCount = maxNrOrStatesInput.get();
        mapCodeToStateSet = null;
        codeLength = -1;
        codeMap = null;
        createCodeMapping();
    }

    private void createCodeMapping() {
        ambiguities = listOfAmbiguitiesInput.get().split(" ");
        ambCount = ambiguities.length;
        codeMapping = new ArrayList<String>();
        for (int i=0; i<stateCount; i++) {
            codeMapping.add(Integer.toString(i));
        }
        for (int i=0; i< ambCount; i++) {
            codeMapping.add(ambiguities[i]);
        }
        codeMapping.add(Character.toString(GAP_CHAR));
        codeMapping.add(Character.toString(MISSING_CHAR));

    }

    @Override
    public List<Integer> string2state(String data) throws Exception {
        List<Integer> sequence;
        sequence = new ArrayList<Integer>();
        // remove spaces
        data = data.replaceAll("\\s", "");

        ArrayList<Integer> amb = new ArrayList<>();
        boolean readingAmb=false;
        for (byte c : data.getBytes()) {
            if (!readingAmb) {
                switch (c) {
                    case GAP_CHAR:
                    case MISSING_CHAR:
                        sequence.add(-1);
                        break;
                    case '{':
                        readingAmb = true;
                        amb.clear();
                        break;
                    default:
                        sequence.add(Integer.parseInt((char) c + ""));
                }
            } else {
                if (c != '}') {
                    amb.add(Integer.parseInt((char) c + "") );
                } else {
                    readingAmb = false;
                    Collections.sort(amb);
                    String ambStr = "";
                    for (int i=0; i<amb.size(); i++) {
                        ambStr += Integer.toString(amb.get(i));
                    }
                    sequence.add(codeMapping.indexOf(ambStr));
                }

            }

        }

        return sequence;
    } // string2state

    @Override
    public String getTypeDescription() {
        return "standard";
    }

    @Override
    public boolean isAmbiguousState(int state) {
        return state < 0;
    }

    @Override
    public char getChar(int state) {
        if (state < 0) {
            return '?';
        }
        return (char)('0'+state);
    }

    @Description("A class to store the description of a character")
    public class CharStateLabels {
        private int nrOfStates;
        private String description;

        public CharStateLabels(int newNrOfStates, String newDescription) {
            nrOfStates = newNrOfStates;
            description = newDescription;
        }

        public int getNrOfStates() {
            return nrOfStates;
        }

        public String getDescription() {
            return description;
        }
    }
}

@@ -513,36 +513,11 @@
 ent%22
-, Input.Validate.REQUIRED
 );%0A
+
     
@@ -1038,16 +1038,21 @@
 iguities
+ = %7B%7D
 ;%0A    pr
@@ -1157,46 +1157,155 @@
     
-stateCount = maxNrOrStatesInput.get();
+if (maxNrOrStatesInput.get() != null) %7B%0A            stateCount = maxNrOrStatesInput.get();%0A        %7D else %7B%0A            stateCount = -1;%0A        %7D%0A
 %0A   
@@ -1455,24 +1455,80 @@
 Mapping() %7B%0A
+        if (listOfAmbiguitiesInput.get() != null) %7B%0A    
         ambi
@@ -1574,24 +1574,35 @@
 split(%22 %22);%0A
+        %7D%0A%0A
         ambC

check if inputs are not null