/*
 * Copyright 1999-2017 Alibaba Group Holding Ltd.
 *
 * 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.github.quartzweb.utils.json;

import com.github.quartzweb.utils.ExceptionUtils;

import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

public class JSONWriter {

    private StringBuilder out;



    public JSONWriter(){
        this.out = new StringBuilder();
    }

    public void writeArrayStart() {
        write('[');
    }

    public void writeComma() {
        write(',');
    }

    public void writeArrayEnd() {
        write(']');
    }
    
    public void writeNull() {
        write("null");
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void writeObject(Object o) {
        if (o == null) {
            writeNull();
            return;
        }

        else if (o instanceof String) {
            writeString((String) o);
            return;
        }

        else if (o instanceof Number) {
            write(o.toString());
            return;
        }

        else if (o instanceof Boolean) {
            write(o.toString());
            return;
        }

        else if (o instanceof Date) {
            writeDate((Date) o);
            return;
        }

        else if (o instanceof Collection) {
            writeArray((Collection) o);
            return;
        }

        else if (o instanceof Throwable) {
            writeError((Throwable) o);
            return;
        }

        else  if (o instanceof int[]) {
            int[] array = (int[]) o;
            write('[');
            for (int i = 0; i < array.length; ++i) {
                if (i != 0) {
                    write(',');
                }
                write(array[i]);
            }
            write(']');
            return;
        }

        else if (o instanceof long[]) {
            long[] array = (long[]) o;
            write('[');
            for (int i = 0; i < array.length; ++i) {
                if (i != 0) {
                    write(',');
                }
                write(array[i]);
            }
            write(']');
            return;
        }

        else if (o instanceof TabularData) {
            writeTabularData((TabularData) o);
            return;
        }

        else if (o instanceof CompositeData) {
            writeCompositeData((CompositeData) o);
            return;
        }

        else if (o instanceof Map) {
            writeMap((Map) o);
            return;
        }
        else {
            Object o1 = new Object();
            if (o == o1) {
                write("null");
                return;
            }
            if (o instanceof Object[]) {
                Object[] oArr = (Object[]) o;
                write('[');
                for (int i = 0; i < oArr.length; i++) {
                    Object o2 = oArr[i];
                    if (i != 0) {
                        write(',');
                    }
                    writeObject(o2);
                }
                write(']');

            } else {
                try {
                    String objStr = o.getClass().getName();
                    writeString(objStr);
                    return;
                } catch (Exception e) {
                    throw new IllegalArgumentException("not support type : " + o.getClass());
                }
            }

        }


        //throw new IllegalArgumentException("not support type : " + o.getClass());
    }

    public void writeDate(Date date) {
        if (date == null) {
            writeNull();
            return;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        writeString(dateFormat.format(date));
    }

    public void writeError(Throwable error) {
        if (error == null) {
            writeNull();
            return;
        }

        write("{\"Class\":");
        writeString(error.getClass().getName());
        write(",\"Message\":");
        writeString(error.getMessage());
        write(",\"StackTrace\":");
        writeString(ExceptionUtils.getStackTrace(error));
        write('}');
    }

    public void writeArray(Object[] array) {
        if (array == null) {
            writeNull();
            return;
        }

        write('[');

        for (int i = 0; i < array.length; ++i) {
            if (i != 0) {
                write(',');
            }
            writeObject(array[i]);
        }

        write(']');
    }

    public void writeArray(Collection<Object> list) {
        if (list == null) {
            writeNull();
            return;
        }

        int entryIndex = 0;
        write('[');

        for (Object entry : list) {
            if (entryIndex != 0) {
                write(',');
            }
            writeObject(entry);
            entryIndex++;
        }

        write(']');
    }

    public void writeString(String text) {
        if (text == null) {
            writeNull();
            return;
        }
        
        write('"');
        for (int i = 0; i < text.length(); ++i) {
            char c = text.charAt(i);
            if (c == '"') {
                write("\\\"");
            } else if (c == '\n') {
                write("\\n");
            } else if (c == '\r') {
                write("\\r");
            } else if (c == '\\') {
                write("\\\\");
            } else if (c == '\t') {
                write("\\t");
            } else if (c < 16) {
                write("\\u000");
                write(Integer.toHexString(c));
            } else if (c < 32) {
                write("\\u00");
                write(Integer.toHexString(c));
            } else if (c >= 0x7f && c <= 0xA0) {
                write("\\u00");
                write(Integer.toHexString(c));
            } else {
                write(c);
            }
        }
        write('"');
    }

    public void writeTabularData(TabularData tabularData) {
        if (tabularData == null) {
            writeNull();
            return;
        }

        int entryIndex = 0;
        write('[');

        for (Object item : tabularData.values()) {
            if (entryIndex != 0) {
                write(',');
            }
            CompositeData row = (CompositeData) item;
            writeCompositeData(row);

            entryIndex++;
        }
        write(']');
    }

    public void writeCompositeData(CompositeData compositeData) {
        if (compositeData == null) {
            writeNull();
            return;
        }

        int entryIndex = 0;
        write('{');

        for (Object key : compositeData.getCompositeType().keySet()) {
            if (entryIndex != 0) {
                write(',');
            }
            writeString((String) key);
            write(':');
            Object value = compositeData.get((String) key);
            writeObject(value);
            entryIndex++;
        }

        write('}');
    }

    public void writeMap(Map<String, Object> map) {
        if (map == null) {
            writeNull();
            return;
        }

        int entryIndex = 0;
        write('{');
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entryIndex != 0) {
                write(',');
            }

            writeString(entry.getKey());
            write(':');
            writeObject(entry.getValue());

            entryIndex++;
        }

        write('}');
    }

    protected void write(String text) {
        out.append(text);
    }

    protected void write(char c) {
        out.append(c);
    }

    protected void write(int c) {
        out.append(c);
    }

    protected void write(long c) {
        out.append(c);
    }

    public String toString() {
        return out.toString();
    }

    /**
     * 是否支持转换
     * @param o 对象
     * @return
     */
    public static boolean support(Object o) {
        if (o == null) {
            return true;
        }

        if (o instanceof String) {
            return true;
        }

        if (o instanceof Number) {
            return true;
        }

        if (o instanceof Boolean) {
            return true;
        }

        if (o instanceof Date) {
            return true;
        }

        if (o instanceof Collection) {
            return true;
        }

        if (o instanceof Throwable) {
            return true;
        }

        if (o instanceof int[]) {
            return true;
        }

        if (o instanceof long[]) {
            return true;
        }

        if (o instanceof TabularData) {
            return true;
        }

        if (o instanceof CompositeData) {
            return true;
        }

        if (o instanceof Map) {
            return true;
        }
        Object o1 = new Object();
        if (o == o1) {
            return true;
        }

        return false;
    }
}