package com.brilliance.isc.swift.swifteditor.tag;

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

public class TagCell {
    public final static String CONST = "CONST";
    public final static String FUNC = "FUNC";

    public boolean fixed;
    public int length;
    public int minLen;
    public String type;
    public int cnt;
    public String content;
    public int line;
    public String value;
    public List<String> valueList = new ArrayList<>();

    public TagCell clone() {
        TagCell cell = new TagCell();
        cell.fixed = fixed;
        cell.length = length;
        cell.type = type;
        cell.cnt = cnt;
        cell.content = content;
        cell.line = line;
        cell.value = value;
        cell.valueList = valueList;
        return cell;
    }

    public void setValue(String amt) {
        if ("d".equals(type) || ("AMOUNT".equals(content) && "FUNC".equals(type))) {
            if (amt.endsWith(","))
                this.value = amt.substring(0, amt.length() - 1);
            else
                this.value = amt.replaceAll(",", ".");
            return;
        }
        this.value = amt;
    }

    public String toReg() {
        return toReg(true,false);
    }

    public String toReg(boolean useRegName,boolean ignoreLine) {
        String cnt = "S" + this.cnt;
        if (CONST.equals(type)) {
            if ("CRLF".equals(content))
                return "[\\r\\n]{1,2}";
            return content;
        } else if (FUNC.equals(type)) {
            if ("AMOUNT".equals(content)) {
                if (useRegName)
                    return "(?<" + cnt + ">[\\d,.]{1," + this.length + "})";
                else
                    return "[\\d,.]{1," + this.length + "}";
            } else if ("NUMBER".equals(content)) {
                if (useRegName)
                    return "(?<" + cnt + ">[\\d,]{1," + this.length + "})";
                else
                    return "[\\d,]{1," + this.length + "}";
            } else if ("HHMM".equals(content)) //hhmm
            {
                if (useRegName)
                    return "(?<" + cnt + ">\\d{4})";
                else
                    return "\\d{4}";
            } else if ("TIME2".equals(content)) {
                if (useRegName)
                    return "(?<" + cnt + ">\\d{6})";
                else
                    return "\\d{6}";
            } else if ("TIME3".equals(content)) {
                if (useRegName)
                    return "(?<" + cnt + ">\\d{2}\\d{2}?)";
                else
                    return "\\d{2}\\d{2}?";
            } else if ("OFFSET".equals(content))//hhmm
            {
                if (useRegName)
                    return "(?<" + cnt + ">\\d{4})";
                else
                    return "\\d{4}";
            } else if ("SIGN".equals(content)) // +/-
            {
                if (useRegName)
                    return "(?<" + cnt + ">\\+|\\-)";
                else
                    return "\\+|\\-";
            } else if ("DATE1".equals(content)) //yyMMdd
            {
                if (useRegName)
                    return "(?<" + cnt + ">\\d{4})";
                else
                    return "\\d{4}";
            } else if ("DATE2".equals(content)) //yyMMdd
            {
                if (useRegName)
                    return "(?<" + cnt + ">\\d{6})";
                else
                    return "\\d{6}";
            } else if ("YEAR".equals(content)) {
                if (useRegName)
                    return "(?<" + cnt + ">\\d{4})";
                else
                    return "\\d{4}";
            } else if ("DATE4".equals(content)) //yyyyMMdd
            {
                if (useRegName)
                    return "(?<" + cnt + ">\\d{8})";
                else
                    return "\\d{8}";
            } else if ("CUR".equals(content)) //yyyyMMdd
            {
                if (useRegName)
                    return "(?<" + cnt + ">[A-Z]{3})";
                else
                    return "[A-Z]{3}";
            } else if ("DC".equals(content)) //yyyyMMdd
            {
                if (useRegName)
                    return "(?<" + cnt + ">C|D|RC|RD)";
                else
                    return "C|D|RC|RD";
            } else if ("SWIFTBIC".equals(content)) //4!a2!a2!c3!c
            {
                if (useRegName)
                    return "(?<" + cnt + ">[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?)";
                else
                    return "[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?";
            } else if ("MT".equals(content)) {
                if (useRegName)
                    return "(?<" + cnt + ">[1-9]\\d{2})";
                else
                    return "[1-9]\\d{2}";
            } else if ("SB-LC".equals(content)) {
                if (useRegName)
                    return "(?<" + cnt + ">[A-Z]{4}[A-Z0-9]{2}\\d{4}[A-Z]{4}[A-Z0-9]{2})";
                else
                    return "[A-Z]{4}[A-Z0-9]{2}\\d{4}[A-Z]{4}[A-Z0-9]{2}";
            } else if ("DM".equals(content)) {
                if (useRegName)
                    return "(?<" + cnt + ">D|M)";
                else
                    return "D|M";
            } else if ("CC".equals(content)) {
                if (useRegName)
                    return "(?<" + cnt + ">[A-Z]{2})";
                else
                    return "[A-Z]{2}";
            } else {
                System.err.println("unknow function:" + content);
            }
        } else {
            String type = "";
            char c = this.type.charAt(0);
            switch (c) {
                case 'n':
                    type = "\\d";
                    break;
                case 'a':
                    type = "[A-Z]";
                    break;
                case 'd':
                    type = "[\\d,.]";
                    break;
                case 'x':
                    type = "[\\w/\\-\\?:\\(\\)\\.,'+\\{\\} ]";
                    break;
                case 'y':
                    type = "[0-9A-Z\\.,\\-\\(\\)/='\\+:\\?\\!\"%&\\*\\<\\>;\\s]";
                    break;
                case 'z':
                    type = "[\\w\\.,\\-\\(\\)/='\\+:\\?!\"%&\\*\\<\\>;\\{\\s@#]";
                    break;
                case 'c':
                    type = "[A-Z0-9]";
                    break;
                case 'h':
                    type = "[A-F0-9]";
                    break;
                case 'e':
                    type = "\\s";
                    break;
                case 'A':
                    type = "[A-Za-z]";
                    break;
                case 'B':
                    type = "[A-Za-z0-9]";
                    break;
                default:
                    System.err.println("unknow type:" + type);
            }
            //String length = "";
            String nolines = "";
            if (fixed || minLen == length) {
                nolines = type + "{" + length + "}";
            } else
                nolines = type + "{" + minLen + "," + length + "}";
            if (!ignoreLine && line > 1) {
                if (useRegName)
                    nolines = nolines + "([\\r\\n]{1,2}" + nolines + "){0," + (line - 1) + "}";
                else    //校验所用的，只转换单行
                {
                    if (fixed || minLen == length) {
                        nolines = type + "{" + length + "}";
                    } else
                        nolines = type + "{" + 0 + "," + length + "}";
                }

            }
            if (useRegName)
                return "(?<" + cnt + ">" + nolines + ")";
            else
                return nolines;
        }
        return null;
    }

}
