/*******************************************************************************
 * Copyright 2017 Bstek
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy
 * of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
package com.bstek.ureport.definition.datasource;

import com.bstek.ureport.Utils;
import com.bstek.ureport.exception.ReportComputeException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author Jacky.gao
 * @since 2016年12月27日
 */
public enum DataType {
    Integer, Float, Boolean, String, Date, List;

    public Object parse(Object obj) {
        if (obj == null) return null;
        switch (this) {
            case Boolean:
                if (obj.toString().equals("")) {
                    return null;
                }
                if (obj instanceof Boolean) {
                    return obj;
                } else {
                    return java.lang.Boolean.valueOf(obj.toString());
                }
            case Float:
                if (obj.toString().equals("")) {
                    return null;
                }
                if (obj instanceof Float) {
                    return obj;
                } else {
                    return Utils.toBigDecimal(obj).doubleValue();
                }
            case Integer:
                if (obj.toString().equals("")) {
                    return null;
                }
                if (obj instanceof Integer) {
                    return obj;
                } else {
                    return Utils.toBigDecimal(obj).intValue();
                }
            case String:
                if (obj instanceof String) {
                    return obj;
                } else {
                    return obj.toString();
                }
            case List:
                if (obj.toString().equals("")) {
                    return null;
                }
                if (obj instanceof List) {
                    return obj;
                } else {
                    String[] arrs = obj.toString().split(",");
                    return new ArrayList<>(Arrays.asList(arrs));
                }
            case Date:
                if (obj.toString().equals("")) {
                    return null;
                }
                if (obj instanceof Date) {
                    return obj;
                } else {
                    Date date;
                    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    try {
                        date = sd.parse(obj.toString());
                    } catch (ParseException e) {
                        sd = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            date = sd.parse(obj.toString());
                        } catch (ParseException ex) {
                            throw new ReportComputeException("Date parameter value pattern must be \"yyyy-MM-dd\" or \"yyyy-MM-dd HH:mm:ss\".");
                        }
                    }
                    return date;
                }
        }
        throw new ReportComputeException("Unknow parameter type : " + this);
    }
}
