package com.kakao.network;

import com.bytedance.covode.number.Covode;
import com.kakao.network.callback.ResponseCallback;
import com.kakao.network.response.ApiResponseStatusError;
import com.kakao.network.response.CustomErrorConverter;
import com.kakao.network.response.ResponseBody;
import com.kakao.network.response.ResponseData;
import com.kakao.network.response.ResponseStringConverter;
import com.kakao.network.tasks.ITaskQueue;
import com.kakao.network.tasks.KakaoResultTask;
import com.kakao.util.helper.log.Logger;
import java.util.List;
import java.util.concurrent.Future;

public class DefaultNetworkService implements NetworkService {
    public ITaskQueue taskQueue;

    static {
        Covode.recordClassIndex(45866);
    }

    public DefaultNetworkService(ITaskQueue iTaskQueue) {
        this.taskQueue = iTaskQueue;
    }

    public <T> T request(IRequest iRequest, ResponseStringConverter<T> responseStringConverter) {
        ResponseData request = new NetworkTask().request(iRequest);
        Logger.d(request.getStringData());
        if (request.getHttpStatusCode() == 200) {
            return responseStringConverter.convert(request.getStringData());
        }
        ResponseBody responseBody = new ResponseBody(request.getStringData());
        throw new ApiResponseStatusError(responseBody.getInt("code"), responseBody.optString("msg", ""), request.getHttpStatusCode(), responseBody);
    }

    public <T> List<T> requestList(IRequest iRequest, ResponseStringConverter<T> responseStringConverter) {
        ResponseData request = new NetworkTask().request(iRequest);
        Logger.d(request.getStringData());
        if (request.getHttpStatusCode() == 200 || request.getHttpStatusCode() == 200) {
            return responseStringConverter.convertList(request.getStringData());
        }
        ResponseBody responseBody = new ResponseBody(request.getStringData());
        throw new ApiResponseStatusError(responseBody.getInt("code"), responseBody.optString("msg", ""), request.getHttpStatusCode(), responseBody);
    }

    public <T> Future<T> request(final IRequest iRequest, final ResponseStringConverter<T> responseStringConverter, ResponseCallback<T> responseCallback) {
        return this.taskQueue.addTask(new KakaoResultTask<T>(responseCallback) {
            static {
                Covode.recordClassIndex(45867);
            }

            public T call() {
                return DefaultNetworkService.this.request(iRequest, responseStringConverter);
            }
        });
    }

    public <T> Future<List<T>> requestList(final IRequest iRequest, final ResponseStringConverter<T> responseStringConverter, ResponseCallback<List<T>> responseCallback) {
        return this.taskQueue.addTask(new KakaoResultTask<List<T>>(responseCallback) {
            static {
                Covode.recordClassIndex(45869);
            }

            public List<T> call() {
                return DefaultNetworkService.this.requestList(iRequest, responseStringConverter);
            }
        });
    }

    public <T, E extends Exception> T request(IRequest iRequest, ResponseStringConverter<T> responseStringConverter, CustomErrorConverter<E> customErrorConverter) {
        ResponseData request = new NetworkTask().request(iRequest);
        Logger.d(request.getStringData());
        if (request.getHttpStatusCode() == 200) {
            return responseStringConverter.convert(request.getStringData());
        }
        throw ((Throwable) customErrorConverter.convert(request.getHttpStatusCode(), request.getStringData()));
    }

    public <T, E extends Exception> Future<T> request(IRequest iRequest, ResponseStringConverter<T> responseStringConverter, CustomErrorConverter<E> customErrorConverter, ResponseCallback<T> responseCallback) {
        final ResponseStringConverter<T> responseStringConverter2 = responseStringConverter;
        final IRequest iRequest2 = iRequest;
        final CustomErrorConverter<E> customErrorConverter2 = customErrorConverter;
        return this.taskQueue.addTask(new KakaoResultTask<T>(responseCallback) {
            static {
                Covode.recordClassIndex(45868);
            }

            public T call() {
                return DefaultNetworkService.this.request(iRequest2, responseStringConverter2, customErrorConverter2);
            }
        });
    }
}
