package com.inspect.patrol.linkage.block;

import com.inspect.patrol.annotation.CimeAnnotation;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CimeProcessor {
    public static List<IBlock> parse(String cime, Class<? extends IBlock> iBlockClass) throws Exception {
        List<IBlock> iBlocks = new ArrayList<>();
        boolean isClassRetrieval = false;
        boolean isVerticalTable = false;
        int verticalIndex = 0;
        BufferedReader reader = new BufferedReader(new StringReader(cime));

        ArrayList var12;
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                if (!line.trim().startsWith("//") && (!line.startsWith("<!") || !line.endsWith("!>"))) {
                    if (isClassRetrieval && line.startsWith("</")) {
                        isClassRetrieval = false;
                    } else if (line.startsWith("<")) {
                        isClassRetrieval = true;
                    } else if ((!isClassRetrieval || !line.startsWith("@@")) && !line.startsWith("@#")) {
                        if (isClassRetrieval && line.startsWith("@")) {
                            isVerticalTable = false;
                        } else if (isClassRetrieval && isVerticalTable) {
                            ++verticalIndex;
                        } else if (isClassRetrieval) {
                            IBlock iBlock = loadHorizonTable(iBlockClass, line);
                            iBlocks.add(iBlock);
                        }
                    } else {
                        verticalIndex = 0;
                        isVerticalTable = true;
                    }
                }
            }

            var12 = (ArrayList) iBlocks;
        } finally {
            reader.close();
        }

        return var12;
    }

    public static String combine(List<? extends IBlock> blocks) throws Exception {
        IBlock iBlock = (IBlock) blocks.get(0);
        Class<? extends IBlock> iBlockClass = ((IBlock) blocks.get(0)).getClass();
        String annotation = combineAnnotation(iBlockClass);
        String fields = combineFields(blocks);
        CimeXmlFormat cimeXmlFormat = ((ICimeXml) iBlock).toCimeXmlFormat();
        return String.format("%s%s%s%s%s", new Object[]{cimeXmlFormat.getHeader(), cimeXmlFormat.getClassBegin(), annotation, fields, cimeXmlFormat.getClassEnd()});
    }

    public static void main(String[] args) throws Exception {
        List<ReverseBlock> list = new ArrayList();
        ReverseBlock reverseBlock = new ReverseBlock();
        reverseBlock.setMonitorIndexNo("1");
        reverseBlock.setDeviceType("1");
        reverseBlock.setDeviceName("1");
        reverseBlock.setSerialNo("1");
        list.add(reverseBlock);
        String combine = combine(list);
        FileOutputStream fileOutputStream = new FileOutputStream("E:/2.cime");
        fileOutputStream.write(combine.getBytes(Charset.forName("GB2312")));
        fileOutputStream.close();
    }

    private static String combineAnnotation(Class<? extends IBlock> iBlockClass) throws Exception {
        Field[] declaredFields = iBlockClass.getDeclaredFields();
        StringBuffer sb = new StringBuffer("@");
        Field[] var3 = declaredFields;
        int var4 = declaredFields.length;

        for (int var5 = 0; var5 < var4; ++var5) {
            Field field = var3[var5];
            CimeAnnotation annotation = (CimeAnnotation) field.getAnnotation(CimeAnnotation.class);
            String value = annotation.value();
            sb.append(value).append("\t");
        }

        return String.format("%s%s", new Object[]{sb.substring(0, sb.length() - 1), "\n"});
    }

    private static String combineFields(List<? extends IBlock> iBlocks) throws Exception {
        StringBuffer sb = new StringBuffer();
        Iterator var2 = iBlocks.iterator();

        while (var2.hasNext()) {
            IBlock iBlock = (IBlock) var2.next();
            String field = combineField(iBlock);
            sb.append(field);
        }

        return sb.toString();
    }

    private static String combineField(IBlock iBlock) throws Exception {
        Field[] declaredFields = iBlock.getClass().getDeclaredFields();
        StringBuffer sb = new StringBuffer("#");

        for (int i = 0; i < declaredFields.length; ++i) {
            Field field = declaredFields[i];
            field.setAccessible(true);
            String value = (String) field.get(iBlock);
            sb.append(value);
            if (i < declaredFields.length - 1) {
                sb.append("\t");
            }
        }

        sb.append("\n");
        return sb.toString();
    }

    private static String[] splitTableColumn(String column) {
        return column.replace("#", "").trim().split("\\t");
    }

    private static IBlock loadHorizonTable(Class<? extends IBlock> iBlockClass, String data) throws Exception {
        String[] args = splitTableColumn(data);
        IBlock iBlock = (IBlock) iBlockClass.newInstance();
        Field[] declaredFields = iBlock.getClass().getDeclaredFields();

        for (int i = 0; i < declaredFields.length; ++i) {
            declaredFields[i].setAccessible(true);
            declaredFields[i].set(iBlock, args[i]);
        }

        return iBlock;
    }
}
