package com.duben.microtribe.net;

import com.google.gson.Gson;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.Charset;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okio.Buffer;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * v6 gson
 */
public class V6GsonConverterFactory extends Converter.Factory {

    private final Gson gson;

    public static V6GsonConverterFactory create() {
        return create(new Gson());
    }

    public static V6GsonConverterFactory create(Gson gson) {
        return new V6GsonConverterFactory(gson);
    }

    private V6GsonConverterFactory(Gson gson) {
        if (gson == null) throw new NullPointerException("gson == null");
        this.gson = gson;
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                                                            Retrofit retrofit) {
        return new GsonResponseBodyConverter<>(gson, type);
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type,
                                                          Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        return new GsonRequestBodyConverter<>(gson, type);
    }

    // 这里创建从ResponseBody其它类型的Converter
    // 主要用于对响应体的处理
    final class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
        private final Gson gson;
        private final Type type;

        GsonResponseBodyConverter(Gson gson, Type type) {
            this.gson = gson;
            this.type = type;
        }

        @Override
        public T convert(ResponseBody value) throws IOException {

            Reader reader = value.charStream();
            try {
                T t = gson.fromJson(reader, type);
//                loginInvalid(t);
                return t;
            } finally {
                try {
                    if (reader != null)
                        reader.close();
                } catch (IOException ignored) {
                    ignored.printStackTrace();
                }
            }
        }

    }

    // 在这里创建 从自定类型到ResponseBody 的Converter
    // 主要用于对Part、PartMap、Body注解的处理
    final class GsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
        private final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
        private final Charset UTF_8 = Charset.forName("UTF-8");

        private final Gson gson;
        private final Type type;

        GsonRequestBodyConverter(Gson gson, Type type) {
            this.gson = gson;
            this.type = type;
        }

        @Override
        public RequestBody convert(T value) throws IOException {

            Buffer buffer = new Buffer();
            Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
            gson.toJson(value, type, writer);
            writer.flush();
            return RequestBody.create(MEDIA_TYPE, buffer.readByteString());

        }
    }

}
