package com.infinite.focus.server.auth;

import java.io.IOException;

import org.springframework.stereotype.Service;

import com.infinite.focus.server.payment.LicensesEnableDisableResponse;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

@Service
public class PayKickStartService implements APIConfiguration {

	private static final String auth_token= "5Aj2eREdJYfa";
	
    private RepositoryInterface service;

    public PayKickStartService() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(PAYKICKSTART_API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        service = retrofit.create(RepositoryInterface.class);
    }

    public LicensesStatusResponse getLicensesStatus(String license_key) throws IOException {
    	Call<LicensesStatusResponse> retrofitCall = service.licensesStatus(auth_token, license_key);

        Response<LicensesStatusResponse> response = retrofitCall.execute();

        if (!response.isSuccessful()) {
            throw new IOException(response.errorBody() != null
                    ? response.errorBody().string() : "Unknown error");
        }

        return response.body();
    }
 
    public LicensesEnableDisableResponse enableLicenses(String license_key) throws IOException {
    	Call<LicensesEnableDisableResponse> retrofitCall = service.enableLicenses(auth_token, license_key);

        Response<LicensesEnableDisableResponse> response = retrofitCall.execute();

        if (!response.isSuccessful()) {
            throw new IOException(response.errorBody() != null
                    ? response.errorBody().string() : "Unknown error");
        }

        return response.body();
    }
    
    public LicensesEnableDisableResponse disableLicenses(String license_key) throws IOException {
    	Call<LicensesEnableDisableResponse> retrofitCall = service.disableLicenses(auth_token, license_key);

        Response<LicensesEnableDisableResponse> response = retrofitCall.execute();

        if (!response.isSuccessful()) {
            throw new IOException(response.errorBody() != null
                    ? response.errorBody().string() : "Unknown error");
        }

        return response.body();
    }
}

