package com.brilliance.isc.doc.gidtxt.format;

import com.brilliance.isc.bo.Bch;
import com.brilliance.isc.bo.model.Gidgrp;
import com.brilliance.isc.bo.model.Nidgrp;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.doc.IGidTextFormat;
import com.brilliance.isc.doc.gidtxt.annotation.TextPath;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

public abstract class AbstractGidTextFormat<V> implements IGidTextFormat<V> {

    public final String EMPTY = "#EMPTY#";

    @Resource
    private PtsptaToolComponent ptsptaToolComponent;

    @Override
    public Map<String, Object> getFormatData(V grpData) {
        //填充数据
        fillGrpData(grpData);
        //开始计算
        Map<String, Object> objectMap = new HashMap<>();
        List<Method> doMethods = new ArrayList<>();
        Method[] methods = this.getClass().getMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(TextPath.class)) {
                doMethods.add(method);
            }
        }
        if (doMethods.size() > 0) {
            Collections.sort(doMethods, Comparator.comparingInt(m -> m.getAnnotation(TextPath.class).order()));
        }
        for (Method method : doMethods) {
            String path = method.getAnnotation(TextPath.class).value().toLowerCase().replaceAll("\\\\", ".");
            try {
                Object value = method.invoke(this, grpData);
                if (EMPTY.equals(value)) {
                    continue;
                }
                objectMap.put(path, value);
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }
        return objectMap;
    }

    public void fillGrpData(V grpData) {

        Nidgrp nidgrp = Utils.getFieldValue(grpData, "nidgrp");
        if (nidgrp != null) {
            fillNidgrp(grpData, nidgrp);
            return;
        }
        Gidgrp gidgrp = Utils.getFieldValue(grpData, "gidgrp");

        if (gidgrp != null) {
            fillGidgrp(grpData, gidgrp);
        }
    }

    public void fillNidgrp(V grpData, Nidgrp nidgrp) {
        ptsptaToolComponent.getPtsptaFromGroup(nidgrp, true).forEach(ptsptaVo -> {
            ptsptaToolComponent.fillPtsptaAllProperties(ptsptaVo);
        });
    }

    public void fillGidgrp(V grpData, Gidgrp gidgrp) {
        ptsptaToolComponent.getPtsptaFromGroup(gidgrp, true).forEach(ptsptaVo -> {
            ptsptaToolComponent.fillPtsptaAllProperties(ptsptaVo);
        });
    }

    public Bch getSettleBch() {
        if (SettleContext.getUserSession().getBch() == null) {
            return new Bch();
        }
        return SettleContext.getUserSession().getBch();
    }

    public String numberBreakWord(String num){
        if (null == num || !num.contains(",")) {
            return num;
        }
        return "<span style=\"word-warp: break-word;\">" + num + "</span>";
    }
}
