/*
 * Copyright 2017 .
 *
 * 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.dinect.checker.net;

import android.support.annotation.NonNull;

import java.util.concurrent.TimeUnit;

import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by anonymous
 */
public final class ApiClient {

    private static final String TAG = "Checker.ApiClient";

    private static final int TIMEOUT = 3;

    private OkHttpClient mHttp;
    private String mEndpoint;

    public ApiClient(final String url, final @NonNull String appToken, final @NonNull String token) {
        mEndpoint = url;
        mHttp = new OkHttpClient().
                newBuilder()
                .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
                .addInterceptor(new DinectAuthorizationInterceptor(appToken, token, "checker/0.1", true))
                .build();
    }

    public void findUser(String card, Callback callback) {
        final Request.Builder requestBuilder = new Request.Builder();
        final HttpUrl url = HttpUrl.parse(mEndpoint);
        if (url != null) {
            HttpUrl.Builder httpBuilder = url.newBuilder().addQueryParameter("auto", card);
            mHttp.newCall(requestBuilder.url(httpBuilder.build()).build()).enqueue(callback);
        }
    }
}
