package com.neptune.plugin.models;

import com.pax.dal.entity.EFontTypeAscii;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PrintBlockModel {

    private PrintBlockType type;
    private EFontTypeAscii asciiFontType;
    private int leftIndent;
    private int wordSpace;
    private int lineSpace;
    private int greyLevel;
    private boolean doubleWidth;
    private boolean doubleHeight;
    private boolean isInvert;
    private int step;
    private String[] data;

    public PrintBlockModel(PrintBlockType type, EFontTypeAscii asciiFontType, int leftIndent, int wordSpace, int lineSpace, int greyLevel, boolean doubleWidth, boolean doubleHeight, boolean isInvert, int step, String[] data) {
        this.type = type;
        this.asciiFontType = asciiFontType;
        this.leftIndent = leftIndent;
        this.wordSpace = wordSpace;
        this.lineSpace = lineSpace;
        this.greyLevel = greyLevel;
        this.doubleWidth = doubleWidth;
        this.doubleHeight = doubleHeight;
        this.isInvert = isInvert;
        this.step = step;
        this.data = data;
    }

    public PrintBlockModel(JSONObject json) throws JSONException {
        this(
                PrintBlockType.getPrintBlockType(json.getString("type")),
                getEFontTypeAscii(json.getString("asciiFontType")),
                json.getInt("leftIndent"),
                json.getInt("wordSpace"),
                json.getInt("lineSpace"),
                json.getInt("greyLevel"),
                json.getBoolean("doubleWidth"),
                json.getBoolean("doubleHeight"),
                json.getBoolean("isInvert"),
                json.getInt("step"),
                jsonArrayToArray(json.getJSONArray("data"))
        );
    }

    public PrintBlockType getType() {
        return type;
    }

    public EFontTypeAscii getAsciiFontType() {
        return asciiFontType;
    }

    public int getLeftIndent() {
        return leftIndent;
    }

    public int getWordSpace() {
        return wordSpace;
    }

    public int getLineSpace() {
        return lineSpace;
    }

    public int getGreyLevel() {
        return greyLevel;
    }

    public boolean isDoubleWidth() {
        return doubleWidth;
    }

    public boolean isDoubleHeight() {
        return doubleHeight;
    }

    public boolean isInvert() {
        return isInvert;
    }

    public int getStep() {
        return step;
    }

    public String[] getData() {
        return data;
    }

    private static String[] jsonArrayToArray(JSONArray jsonArray) throws JSONException {
        List<String> list = new ArrayList<String>();
        for (int i=0; i<jsonArray.length(); i++) {
            list.add( jsonArray.getString(i) );
        }
        String[] stringArray = list.toArray(new String[list.size()]);
        return stringArray;
    }

    private static EFontTypeAscii getEFontTypeAscii(String asciiType){
        for (EFontTypeAscii type : EFontTypeAsciiValues()) {
            if (type.name().equals(asciiType)) {
                return type;
            }
        }
        return null;
    }

    private static EFontTypeAscii[] EFontTypeAsciiValues(){
        return new EFontTypeAscii[]{
                EFontTypeAscii.FONT_8_16,
                EFontTypeAscii.FONT_16_24,
                EFontTypeAscii.FONT_12_24,
                EFontTypeAscii.FONT_8_32,
                EFontTypeAscii.FONT_16_48,
                EFontTypeAscii.FONT_12_48,
                EFontTypeAscii.FONT_16_16,
                EFontTypeAscii.FONT_32_24,
                EFontTypeAscii.FONT_24_24,
                EFontTypeAscii.FONT_16_32,
                EFontTypeAscii.FONT_32_48,
                EFontTypeAscii.FONT_24_48
        };
    }



    public enum PrintBlockType {
        BITMAP,
        TEXT;

        private static PrintBlockType getPrintBlockType(String modeType){
            for (PrintBlockType type : values()) {
                if (type.name().equals(modeType)) {
                    return type;
                }
            }
            return null;
        }
    }



}
