package com.example.iot_controlhost.utils.network;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.blankj.utilcode.util.GsonUtils;
import com.example.iot_controlhost.model.net.CommonResponse;
import com.example.iot_controlhost.utils.log.MyLog;

import org.json.JSONException;
import org.xutils.common.Callback;

import java.lang.reflect.ParameterizedType;

/**
 * @Author：BinJianXin
 * @Date：2022/5/30/9:49
 * @Declaration：用于减少逻辑界面xhttp需要实现的方法数
 */
public abstract class XHttpShorthand<T>  implements Callback.CommonCallback<String> {

    public abstract void success(T result) throws JSONException;

    private Class<T> clazz;

    public XHttpShorthand() {
        try {
            ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
            clazz = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public abstract void error(Throwable e);

    @Override
    public void onSuccess(String result) {
        MyLog.network("网络请求成功：" + result);
        Message message = new Message();
        try {
            CommonResponse temp = GsonUtils.fromJson(result, CommonResponse.class);
            MyLog.network("已转为通用请求内容");
            if (temp != null) {
//                MyLog.network("Code == 200");
                message.what = 0;
                message.obj = GsonUtils.fromJson(result, clazz);
            } else {
//                MyLog.network("Code != 200");
                message.what = 2;
                message.obj = temp;
            }
        } catch (Exception e) {
            message.what = 1;
            message.obj = e;
        }
        handler.sendMessage(message);
    }

    public void onSuccessError(CommonResponse data) {

    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        MyLog.networkError("网络请求失败：错误消息：" + ex.getMessage()
                + "\n本地化消息：" + ex.getLocalizedMessage()
                + "\n原因：" + ex.getCause());
        Message message = new Message();
        message.what = 1;
        message.obj = ex;
        handler.sendMessage(message);
    }

    @Override
    public void onCancelled(CancelledException cex) {
        MyLog.network("网络请求取消" + cex.getMessage());
    }

    @Override
    public void onFinished() {
        Message message = new Message();
        message.what = 3;
        handler.sendMessage(message);
    }

    public void onFinish() {
        MyLog.network("网络请求完成");
    }

    /**
     * 转回到主线程
     */
    public Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@androidx.annotation.NonNull Message msg) {
            super.handleMessage(msg);
            try {
                if (msg.what == 0) {
                    success((T) msg.obj);
                } else if (msg.what == 1) {
                    error((Throwable) msg.obj);
                } else if (msg.what == 2) {
                    onSuccessError((CommonResponse) msg.obj);
                }else if(msg.what == 3){
                    onFinish();
                }
            } catch (Exception e) {
                MyLog.networkError("网络请求错误：" + e.getMessage());
            }
        }
    };
}