package com.example.petstoremobile.di;

import android.content.Context;
import android.os.Build;

import com.example.petstoremobile.BuildConfig;
import com.example.petstoremobile.api.*;
import com.example.petstoremobile.api.auth.AuthApi;
import com.example.petstoremobile.api.auth.AuthInterceptor;
import com.example.petstoremobile.api.auth.TokenManager;

import java.util.concurrent.TimeUnit;

import javax.inject.Named;
import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.qualifiers.ApplicationContext;
import dagger.hilt.components.SingletonComponent;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

//Module to provide dependencies injection for the api
@Module
@InstallIn(SingletonComponent.class)
public class NetworkModule {

    @Provides
    @Singleton
    @Named("baseUrl")
    public static String provideBaseUrl() {
        return isEmulator() ? BuildConfig.EMULATOR_BACKEND_URL : BuildConfig.DEVICE_BACKEND_URL;
    }

    // Check if the device is an emulator
    private static boolean isEmulator() {
        return Build.FINGERPRINT.startsWith("generic")
                || Build.FINGERPRINT.startsWith("unknown")
                || Build.MODEL.contains("google_sdk")
                || Build.MODEL.contains("Emulator")
                || Build.MODEL.contains("Android SDK built for x86")
                || Build.MANUFACTURER.contains("Genymotion")
                || Build.HARDWARE.contains("goldfish")
                || Build.HARDWARE.contains("ranchu")
                || Build.PRODUCT.contains("sdk")
                || Build.PRODUCT.contains("sdk_gphone")
                || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"));
    }

    @Provides
    @Singleton
    public static OkHttpClient provideOkHttpClient(TokenManager tokenManager) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);

        return new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .addInterceptor(new AuthInterceptor(tokenManager))
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .build();
    }

    //build the retrofit instance
    @Provides
    @Singleton
    public static Retrofit provideRetrofit(@Named("baseUrl") String baseUrl, OkHttpClient client) {
        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }

    //associate the api with the retrofit instance
    @Provides
    @Singleton
    public static PetApi providePetApi(Retrofit retrofit) {
        return retrofit.create(PetApi.class);
    }

    @Provides
    @Singleton
    public static ServiceApi provideServiceApi(Retrofit retrofit) {
        return retrofit.create(ServiceApi.class);
    }

    @Provides
    @Singleton
    public static SupplierApi provideSupplierApi(Retrofit retrofit) {
        return retrofit.create(SupplierApi.class);
    }

    @Provides
    @Singleton
    public static AdoptionApi provideAdoptionApi(Retrofit retrofit) {
        return retrofit.create(AdoptionApi.class);
    }

    @Provides
    @Singleton
    public static AppointmentApi provideAppointmentApi(Retrofit retrofit) {
        return retrofit.create(AppointmentApi.class);
    }

    @Provides
    @Singleton
    public static ProductApi provideProductApi(Retrofit retrofit) {
        return retrofit.create(ProductApi.class);
    }

    @Provides
    @Singleton
    public static SaleApi provideSaleApi(Retrofit retrofit) {
        return retrofit.create(SaleApi.class);
    }

    @Provides
    @Singleton
    public static PurchaseOrderApi providePurchaseOrderApi(Retrofit retrofit) {
        return retrofit.create(PurchaseOrderApi.class);
    }

    @Provides
    @Singleton
    public static ProductSupplierApi provideProductSupplierApi(Retrofit retrofit) {
        return retrofit.create(ProductSupplierApi.class);
    }

    @Provides
    @Singleton
    public static InventoryApi provideInventoryApi(Retrofit retrofit) {
        return retrofit.create(InventoryApi.class);
    }

    @Provides
    @Singleton
    public static AuthApi provideAuthApi(Retrofit retrofit) {
        return retrofit.create(AuthApi.class);
    }

    @Provides
    @Singleton
    public static ChatApi provideChatApi(Retrofit retrofit) {
        return retrofit.create(ChatApi.class);
    }

    @Provides
    @Singleton
    public static CustomerApi provideCustomerApi(Retrofit retrofit) {
        return retrofit.create(CustomerApi.class);
    }

    @Provides
    @Singleton
    public static MessageApi provideMessageApi(Retrofit retrofit) {
        return retrofit.create(MessageApi.class);
    }

    @Provides
    @Singleton
    public static StoreApi provideStoreApi(Retrofit retrofit) {
        return retrofit.create(StoreApi.class);
    }

    @Provides
    @Singleton
    public static CategoryApi provideCategoryApi(Retrofit retrofit) {
        return retrofit.create(CategoryApi.class);
    }

    @Provides
    @Singleton
    public static UserApi provideUserApi(Retrofit retrofit) {
        return retrofit.create(UserApi.class);
    }

    @Provides
    @Singleton
    public static EmployeeApi provideEmployeeApi(Retrofit retrofit) {
        return retrofit.create(EmployeeApi.class);
    }

    @Provides
    @Singleton
    public static RefundApi provideRefundApi(Retrofit retrofit) {
        return retrofit.create(RefundApi.class);
    }

    @Provides
    @Singleton
    public static CouponApi provideCouponApi(Retrofit retrofit) {
        return retrofit.create(CouponApi.class);
    }

    @Provides
    @Singleton
    public static ActivityLogApi provideActivityLogApi(Retrofit retrofit) {
        return retrofit.create(ActivityLogApi.class);
    }
}