package cn.com.qmth.stmms.biz.mark.model;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * 单个裁切区设置模型
 * 
 * @author luoshi
 *
 */
public class PictureConfigItem {

    // 数据库中多个单元紧凑保存的拼接符，兼容旧版本只保存图片序号的模式
    public static final String DB_ITEM_JOINER = ",";

    // 数据库中单个单元紧凑保存的拼接符，兼容旧版本只保存图片序号的模式
    public static final String DB_FIELD_JOINER = ":";

    // 裁切去所属的图片序号，从1开始
    private int i;

    // 裁切区左上角X坐标
    private int x;

    // 裁切区左上角Y坐标
    private int y;

    // 裁切区宽度
    private int w;

    // 裁切区高度
    private int h;

    public PictureConfigItem() {

    }

    public PictureConfigItem(int i) {
        this.i = i;
        this.w = 0;
        this.h = 0;
        this.x = 0;
        this.y = 0;
    }

    public PictureConfigItem(String text) {
        text = StringUtils.trimToEmpty(text);
        String[] values = StringUtils.split(text, DB_FIELD_JOINER);
        try {
            i = Integer.valueOf(values[0]);
            if (values.length > 1) {
                x = Integer.valueOf(values[1]);
            }
            if (values.length > 2) {
                y = Integer.valueOf(values[2]);
            }
            if (values.length > 3) {
                w = Integer.valueOf(values[3]);
            }
            if (values.length > 4) {
                h = Integer.valueOf(values[4]);
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("Invalid PictureConfigItem init text:" + text);
        }
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(i).append(DB_FIELD_JOINER);
        sb.append(x).append(DB_FIELD_JOINER);
        sb.append(y).append(DB_FIELD_JOINER);
        sb.append(w).append(DB_FIELD_JOINER);
        sb.append(h);
        return sb.toString();
    }

    public static List<PictureConfigItem> parse(String text) {
        List<PictureConfigItem> list = new LinkedList<PictureConfigItem>();
        text = StringUtils.trimToEmpty(text);
        String[] values = StringUtils.split(text, DB_ITEM_JOINER);
        for (String value : values) {
            try {
                PictureConfigItem item = new PictureConfigItem(value);
                if (item != null && item.i > 0) {
                    list.add(item);
                }
            } catch (Exception e) {
                continue;
            }
        }
        return list;
    }

}
