package com.bohuikeji.frame.module.admin.entity.web.block;


import com.bohuikeji.frame.module.admin.enums.ButtonBlockEnum;
import com.bohuikeji.frame.module.admin.base.HtmlBlockBase;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 按钮元素块
 * @author 10552
 */
@Slf4j
public class ButtonBlock extends HtmlBlockBase<ButtonBlock> {
    public ButtonBlock(Field field){
        super(field);
    }

    /**
     * 事件类型
     */
    private ButtonBlockEnum eventType;
    /**
     * 单击事件
     */
    private String onClick;

    /**
     * 按钮风格，支持bootstrap样式
     */
    private String style;


    public ButtonBlockEnum getEventType() {
        return eventType;
    }

    public ButtonBlock setEventType(ButtonBlockEnum eventType) {
        this.eventType = eventType;
        return this;
    }

    public String getStyle() {
        return style;
    }

    public ButtonBlock setStyle(String style) {
        this.style = style;
        return this;
    }

    public String getOnClick() {
        return onClick;
    }

    public ButtonBlock setOnClick(String onClick) {
        this.onClick = onClick;
        return this;
    }

    /**
     * 创建按钮
     * @param type
     * @param title
     * @return
     */
    public static ButtonBlock createButton(ButtonBlockEnum type,String title){
        ButtonBlock but=new ButtonBlock(null).setEventType(type);
        switch (type){
            case ADD:
                but.setStyle("btn btn-success m-r-5") .setIcon("mdi mdi-check").setTitle(title).setOnClick("buttonClick('ADD')");break;
            case EDIT:
                but.setStyle("btn btn-warning m-r-5") .setIcon("mdi mdi-pencil").setTitle(title).setOnClick("buttonClick('EDIT')");break;
            case DELETE:
                but.setStyle("btn btn-danger m-r-5") .setIcon("mdi mdi-window-close").setTitle(title).setOnClick("buttonClick('DELETE')");break;
            default:
                log.warn("【警告】按钮组件创建错误，已忽略。无法识别的ButtonBlockEnum类型：[{}]",type);break;
        }
        return but;
    }

    /**
     * 静态方法，用于创建按钮组
     * @param types 按钮事件类型组
     * @return
     */
    public static List<ButtonBlock> createButtons(ButtonBlockEnum... types){
        //默认如果不指定类型，则视为添加全部类型按钮
        if (types.length==0){
            types=ButtonBlockEnum.values();
        }
        List<ButtonBlock> butList=new ArrayList<>(types.length);
        ButtonBlock but;
        for (ButtonBlockEnum type:types){
            but=new ButtonBlock(null).setEventType(type);
            switch (type){
                case ADD:
                    butList.add(but.setStyle("btn btn-success m-r-5") .setIcon("mdi mdi-check").setTitle("新增").setOnClick("buttonClick('ADD')"));break;
                case EDIT:
                    butList.add(but.setStyle("btn btn-warning m-r-5") .setIcon("mdi mdi-pencil").setTitle("编辑").setOnClick("buttonClick('EDIT')"));break;
                case DELETE:
                    butList.add(but.setStyle("btn btn-danger m-r-5") .setIcon("mdi mdi-window-close").setTitle("删除").setOnClick("buttonClick('DELETE')"));break;
                    default:
                        /*log.warn("【警告】按钮组件创建错误，已忽略。无法识别的ButtonBlockEnum类型：[{}]",type);*/break;
            }
        }
        return butList;
    }
}
