package com.sunxung.factoring.component.tag;

import java.util.List;

import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.model.IAttribute;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

import com.sunxung.factoring.component.util.SpringUtil;
import com.sunxung.factoring.component.util.StringUtil;
import com.sunxung.factoring.entity.sys.Dictionary;
import com.sunxung.factoring.service.sys.DictionaryService;

/**
 * HTML中checkbox tag标签
 */
public class CheckboxTag extends AbstractAttributeTagProcessor {

    private static String ATTR_NAME = "inputCheck";
    private static int PRECEDENCE = 10000;
    private static DictionaryService dictionaryService;

    public CheckboxTag(final String dialectPrefix){
        super(TemplateMode.HTML, dialectPrefix, null, false, ATTR_NAME, true, PRECEDENCE, true);
    }

    @Override
    protected void doProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName,
            String attributeValue, IElementTagStructureHandler structureHandler) {
        StringBuffer sb = new StringBuffer("");
        IAttribute defultValue = tag.getAttribute("value");
        IAttribute inputName = tag.getAttribute("name");
        IAttribute inputClass = tag.getAttribute("class");
        List<Dictionary> dictionarys = getDictionaryService().findByParentCode(attributeValue);
        if (dictionarys != null) {
            for (Dictionary dict : dictionarys) {
                if (defultValue != null && StringUtil.isNotEmpty(defultValue.getValue())) {
                    String[] dis = defultValue.getValue().split(",");
                    if (null != dis && dis.length > 0) {
                        // 定义标识，表示当前这个字典项是否被选中
                        boolean flag = true;
                        for (String di : dis) {
                            if (dict.getId().equals(di)) {
                                sb.append("<label class='check col-xs-6'><input type='checkbox'" + inputClass
                                        + inputName + " value='" + dict.getId()
                                        + "' checked><span class='lbl'></span><span class='cboxName'>"
                                        + dict.getName() + "</span></input></label>");
                                flag = false;
                            }
                        }
                        if (flag) {
                            sb.append("<label class='check col-xs-6'><input type='checkbox'" + inputClass
                                    + inputName + " value='" + dict.getId()
                                    + "'><span class='lbl'></span><span class='cboxName'>" + dict.getName()
                                    + "</span></input></label>");
                        }
                    }

                } else {
                    sb.append("<label class='check col-xs-6'><input type='checkbox'" + inputClass + inputName
                            + " value='" + dict.getId()
                            + "'><span class='lbl'></span><span class='cboxName'>" + dict.getName()
                            + "</span></input></label>");
                }
            }
        }
        structureHandler.replaceWith(sb.toString(), false);
    }

    private DictionaryService getDictionaryService() {
        if (dictionaryService == null) {
            dictionaryService = SpringUtil.getBeanByType(DictionaryService.class);
        }
        return dictionaryService;
    }
}
