package com.brframework.commoncms.core.layout.column;

import com.brframework.commoncms.annatotion.column.SwitchColumn;
import com.brframework.commoncms.core.layout.AbstractPanelLayoutColumn;
import lombok.Data;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Switch控件
 *
 * @author 揭光智
 * @date 2019/02/16
 * @see SwitchColumn
 */
@Data
public class LayoutSwitchColumn extends AbstractPanelLayoutColumn {

    private static final String TYPE = "SWITCH";
    private String key;
    private String title;

    /**
     * 表示此开关是否打开.默认为false(关闭状态）.
     */
    private boolean value;

    /**
     * 如果为true,这个组件不能进行交互.
     */
    private boolean disable;

    /**
     * 开启状态时的背景颜色.
     */
    private String onTintColor;

    /**
     * 关闭状态时的背景颜色
     */
    private String tintColor;

    /**
     * 开关上圆形按钮的背景颜色
     */
    private String thumbTintColor;

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public String getType() {
        return TYPE;
    }

    private boolean isValue(Field columnField) {
        SwitchColumn switchColumn = AnnotationUtils.findAnnotation(columnField, SwitchColumn.class);
        return switchColumn != null && switchColumn.value();
    }

    private boolean isDisable(Field columnField) {
        SwitchColumn switchColumn = AnnotationUtils.findAnnotation(columnField, SwitchColumn.class);
        return switchColumn != null && switchColumn.disabled();
    }

    private String getOnTintColor(Field columnField) {
        SwitchColumn switchColumn = AnnotationUtils.findAnnotation(columnField, SwitchColumn.class);
        return switchColumn == null ? null : switchColumn.onTintColor();
    }

    private String getTintColor(Field columnField) {
        SwitchColumn switchColumn = AnnotationUtils.findAnnotation(columnField, SwitchColumn.class);
        return switchColumn == null ? null : switchColumn.tintColor();
    }

    private String getThumbTintColor(Field columnField) {
        SwitchColumn switchColumn = AnnotationUtils.findAnnotation(columnField, SwitchColumn.class);
        return switchColumn == null ? null : switchColumn.thumbTintColor();
    }

    @Override
    public void create(Field columnField) throws Throwable {
        init(columnField);

        //设置Switch控件的状态
        setValue(isValue(columnField));
        setDisable(isDisable(columnField));
        setOnTintColor(getOnTintColor(columnField));
        setTintColor(getTintColor(columnField));
        setThumbTintColor(getThumbTintColor(columnField));
    }

    @Override
    public List<Class<?>> supportType() {
        return null;
    }


}
