package com.leanwo.activiti.util;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.time.FastDateFormat;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.google.gson.Gson;
/**
 * @author Administrator
 * */
public class FormatUtil {
    private static String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
    private static String dateFormat = "yyyy-MM-dd";
    private static String timeFormat = "HH:mm:ss";
    private static String dateTimeContinuousFormat = "yyyyMMdd_HHmmss";
    private static String dateFormatWithChinese = "yyyy年MM月dd日";
    private static String dateSimpleTimeFormat = "yy-MM-dd HH:mm:ss";
    private static String yearFormat = "yyyy";
    private static String monthFormat = "yyyy-MM";

    private static DateTimeFormatter defaultDateTimeFormat = DateTimeFormat.forPattern(dateTimeFormat);
    private static DateTimeFormatter defaultDateFormat = DateTimeFormat.forPattern(dateFormat);
    private static DateTimeFormatter defaultTimeFormat = DateTimeFormat.forPattern(timeFormat);
    private static DateTimeFormatter defaultYearFormat = DateTimeFormat.forPattern(yearFormat);
    private static DateTimeFormatter defaultdateTimeContinuousFormat = DateTimeFormat.forPattern(dateTimeContinuousFormat);
    private static DateTimeFormatter defaultDateFormatWithChinese = DateTimeFormat.forPattern(dateFormatWithChinese);
    private static DateTimeFormatter defaultDateSimpleTimeFormat = DateTimeFormat.forPattern(dateSimpleTimeFormat);
    private static DateTimeFormatter defaultMonthFormat = DateTimeFormat.forPattern(monthFormat);

    private static Gson gson = null;
    
    public static final String EMPTY_ARRAY = "[]";
    public static final String ERROR = "ERROR";
    public static final String SUCCESS = "SUCCESS";
    public static final String NULL = "NULL";
    
    public final static String STOCK_IN_LOG_LIST = "STOCK_IN_LOG_LIST";
    public final static String STOCK_IN_VENTORY_LOG_LIST="STOCK_IN_VENTORY_LOG_LIST";
    public final static String STOCK_OUT_LOG_LIST = "STOCK_OUT_LOG_LIST";
    public final static String GOODS_INSTANCE_ORIGINAL_NO = "GOODS_INSTANCE_ORIGINAL_NO";
    public final static String STOCK_MOVEMENT_LOG_LIST = "STOCK_MOVEMENT_LOG_LIST";
    public final static String STOCK_RETURN_LOG_LIST = "STOCK_RETURN_LOG_LIST";
    public final static String PRODUCT_STOCK_IN_LOG_LIST = "PRODUCT_STOCK_IN_LOG_LIST";
    public final static String PRODUCT_STOCK_OUT_LOG_LIST = "PRODUCT_STOCK_OUT_LOG_LIST";
    public final static String PRODUCT_STOCK_MOVE_LOG_LIST = "PRODUCT_STOCK_MOVE_LOG_LIST";
    public final static String PRODUCT_STOCK_RETURN_LOG_LIST = "PRODUCT_STOCK_RETURN_LOG_LIST";
    
    public final static String ACCOUNT_ID = "ACCOUNT_ID";
    public final static String TOKEN = "TOKEN";
    
    public final static Map<String, DecimalFormat> decimalFormatMap = new HashMap<String, DecimalFormat>();
    
    public static DateTimeFormatter getDefaultDateFormatWithChinese() {
        return defaultDateFormatWithChinese;
    }

    public static DateTimeFormatter getDefaultDateTimeFormat() {
        return defaultDateTimeFormat;
    }

    public static DateTimeFormatter getDefaultDateFormat() {
        return defaultDateFormat;
    }

    public static DateTimeFormatter getDefaultTimeFormat() {
        return defaultTimeFormat;
    }
    
    public static DateTimeFormatter getContinuousDateTimeFormat() {
        return defaultdateTimeContinuousFormat;
    }
    
    public static DateTimeFormatter getDefaultDateSimpleTimeFormat() {
        return defaultDateSimpleTimeFormat;
    }
    
    public static DateTimeFormatter getDefaultYearFormat() {
        return defaultYearFormat;
    }
    
    public static DateTimeFormatter getDefaultMonthFormat(){
        return defaultMonthFormat;
    }
    

    /**
     * 获取GSON
     * @return
     */
    public static Gson getGson(){
        if(gson == null) {
            gson = (Gson) SpringUtil.getSingleBean(Gson.class);
        }
        return gson;
    }
    
    public static DecimalFormat getDecimalFormat(String pattern) {
        DecimalFormat decimalFormat = decimalFormatMap.get(pattern);
        if(decimalFormat == null) {
            decimalFormat = new DecimalFormat(pattern);
            decimalFormatMap.put(pattern, decimalFormat);
        }
        return decimalFormat;
    }
    
    public static Date parseDateFormatWithChinese(String str) {
        DateTime dateTime = DateTime.parse(str, defaultDateFormatWithChinese);
        return dateTime.toDate();
    }

    public static Date parseDateTimeFormat(String str) {
        DateTime dateTime = DateTime.parse(str, defaultDateTimeFormat);
        return dateTime.toDate();
    }

    public static Date parseDateFormat(String str) {
        DateTime dateTime = DateTime.parse(str, defaultDateFormat);
        return dateTime.toDate();
    }

    public static Date parseTimeFormat(String str) {
        DateTime dateTime = DateTime.parse(str, defaultTimeFormat);
        return dateTime.toDate();
    }
    
    public static Date parseContinuousDateTimeFormat(String str) {
        DateTime dateTime = DateTime.parse(str, defaultdateTimeContinuousFormat);
        return dateTime.toDate();
    }
    
    public static Date parseSimpleTimeFormat(String str) {
        DateTime dateTime = DateTime.parse(str, defaultDateSimpleTimeFormat);
        return dateTime.toDate();
    }
    
    public static Date parseYearFormat(String str) {
        DateTime dateTime = DateTime.parse(str, defaultYearFormat);
        return dateTime.toDate();
    }
    
    public static Date parseMonthFormat(String str){
        DateTime dateTime = DateTime.parse(str, defaultMonthFormat);
        return dateTime.toDate();
    }
    
    public static String formatDateFormatWithChinese(Date date) {
        return FastDateFormat.getInstance(dateFormatWithChinese).format(date);
    }

    public static String formatDateTimeFormat(Date date) {
        return FastDateFormat.getInstance(dateTimeFormat).format(date);
    }

    public static String formatDateFormat(Date date) {
        return FastDateFormat.getInstance(dateFormat).format(date);
    }

    public static String formatTimeFormat(Date date) {
        return FastDateFormat.getInstance(timeFormat).format(date);
    }
    
    public static String formatContinuousDateTimeFormat(Date date) {
        return FastDateFormat.getInstance(dateTimeContinuousFormat).format(date);
    }
    
    public static String formatSimpleTimeFormat(Date date) {
        return FastDateFormat.getInstance(dateSimpleTimeFormat).format(date);
    }
    
    public static String formatYearFormat(Date date) {
        return FastDateFormat.getInstance(yearFormat).format(date);
    }
    
    public static String formatMonthFormat(Date date){
        return FastDateFormat.getInstance(monthFormat).format(date);
    }
}
