package com.ytpm.hydtw.network;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.ytpm.hydtw.loading.GlobalLoading;
import com.ytpm.hydtw.network.api.Result;

import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * @author mmmmg
 * @description: 网络请求callback
 * @date :2025/06/12 10:22
 */
public abstract class Callback2<T> implements Callback<Result<T>> {

    public Callback2() {
        super();
    }

    @Override
    public void onResponse(Call<Result<T>> call, Response<Result<T>> response) {

        LogUtils.e("dfdfdf", call.request().url());
        LogUtils.e("dfdfdf", response.code());

        GlobalLoading.getInstance().dismiss();

        Result<T> body = response.body();
        if (response.isSuccessful()) {
            if (body != null) {
                LogUtils.e("dfdfdf response body", new Gson().toJson(response.body().getData()));
                int code = body.getCode();
                String reason = body.getMessage();
                if (code == 200) {
                    success(body.getData());
                } else {
                    fail(reason);
                }
                return;
            } else {
                fail("暂无数据!");
                return;
            }
        }

        final String msg;
        switch (response.code()) {
            case 400:
                msg = "参数错误";
                break;
            case 403:
                msg = "禁止访问, 请联系管理员";
                break;
            default:
                msg = "操作失败，请联系管理员";
        }
        fail(msg);
    }

    @Override
    public void onFailure(Call<Result<T>> call, Throwable t) {
        LogUtils.e("Call onFailure" + t.getMessage() +"||");
        GlobalLoading.getInstance().dismiss();

        if (t instanceof HttpError) {
            //用于convert函数直接抛出异常接收
            fail(t.getMessage());
        } else if (t instanceof UnknownHostException) {
            fail("网络异常，请查看手机网络连接");
        } else if (t instanceof ConnectException) {
            fail("网络异常或服务器异常，请查看手机网络连接");
        } else if (t instanceof SocketException) {
            fail("服务异常, 请联系管理员");
        } else if (t instanceof SocketTimeoutException) {
            fail("响应超时");
        } else {
            fail("请求失败");
        }
    }

    public abstract void success(T body);

    public abstract void fail(String msg);

//  private void refreshToken() {
//
//    BaseRetrofit
//        .getInstance()
//        .getApiService()
//        .accessToken(SPUtils.getInstance().getString("token"))
//        .enqueue(new Callback2<RefreshTokenDao>() {
//          @Override
//          public void success(RefreshTokenDao body) {
//            SPUtils.put(MyApplication.getAppContext(), SPEunm.ACCESS_TOKEN, body.getM_Item2());
//          }
//
//          @Override
//          public void fail(String msg) {
//            SPUtils.clear(MyApplication.getAppContext());
//            Intent intent = new Intent(MyApplication.getAppContext(), LoginActivity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
//            MyApplication.getAppContext().startActivity(intent);
//          }
//
//          @Override
//          public void updateToken() {
//
//          }
//        });
//  }
}
