package com.ys.bdtp.adm.di

import android.content.Context
import android.util.Log
import com.ys.bdtp.adm.app.baseUrl
import com.ys.bdtp.adm.app.converter.NullOrEmptyConverter
import com.ys.bdtp.adm.app.interceptor.GzipInterceptor
import com.ys.bdtp.adm.app.interceptor.TokenInterceptor
import com.ys.bdtp.adm.mvvm.model.db.AdmDatabase
import com.ys.bdtp.adm.mvvm.model.nt.Api
import com.ys.bdtp.adm.mvvm.vm.cad.CadRepository
import com.ys.bdtp.adm.mvvm.vm.cad.CadViewModel
import com.ys.bdtp.adm.mvvm.vm.flash.FlashRepository
import com.ys.bdtp.adm.mvvm.vm.flash.FlashViewModel
import com.ys.bdtp.adm.mvvm.vm.login.LoginRepository
import com.ys.bdtp.adm.mvvm.vm.login.LoginViewModel
import com.ys.bdtp.adm.mvvm.vm.main.MainRepository
import com.ys.bdtp.adm.mvvm.vm.main.MainViewModel
import com.ys.bdtp.adm.mvvm.vm.offline.OfflineRepository
import com.ys.bdtp.adm.mvvm.vm.offline.OfflineViewModel
import com.ys.bdtp.adm.mvvm.vm.project.ProjectRepository
import com.ys.bdtp.adm.mvvm.vm.project.ProjectViewModel
import com.ys.bdtp.adm.mvvm.vm.task.TaskRepository
import com.ys.bdtp.adm.mvvm.vm.task.TaskViewModel
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.kodein.di.*
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.UnsupportedEncodingException
import java.util.concurrent.TimeUnit


val httpModule = DI.Module("httpModule") {
    // 构建 Retrofit 对象
    bind {
        singleton {
            Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(instance())
                .addConverterFactory(NullOrEmptyConverter())
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        }
    }

    // 构建 OkHttpClient 对象
    bind {
        singleton {
            OkHttpClient.Builder()
                .connectTimeout(100, TimeUnit.SECONDS)
                .readTimeout(3000, TimeUnit.SECONDS)
                .writeTimeout(3000, TimeUnit.SECONDS)
                .addInterceptor(instance<TokenInterceptor>())
                .addInterceptor(instance<HttpLoggingInterceptor>())
                .addInterceptor(instance<GzipInterceptor>())
                .build()
        }
    }

    //  Logging 拦截器单例
    bind {
        singleton {
            HttpLoggingInterceptor { message ->
                try {
                    Log.d("OKHttp-----", message)
                } catch (e: UnsupportedEncodingException) {
                    e.printStackTrace()
                    Log.d("OKHttp-----", message)
                }
            }.setLevel(HttpLoggingInterceptor.Level.BODY)
        }
    }

    // Gzip 压缩拦截器单例
    bind {
        singleton {
            GzipInterceptor()
        }
    }

    // Token 拦截器单例
    bind {
        singleton {
            TokenInterceptor(on(instance<Context>()).instance())
        }
    }
    // Api 对象
    bind {
        singleton { instance<Retrofit>().create(Api::class.java) }
    }
}

val repositoryModule = DI.Module("repository") {
    bind { provider { FlashRepository(instance(), instance()) } }
    bind { provider { LoginRepository(instance(), instance()) } }
    bind { provider { MainRepository(instance()) } }
    bind { provider { ProjectRepository(instance(), instance()) } }
    bind { provider { TaskRepository(instance(), instance()) } }
    bind { provider { OfflineRepository(instance(), instance()) } }
    bind { provider { CadRepository(instance(), instance()) } }
}

val viewModelModule = DI.Module("viewModel") {
    bind { provider { FlashViewModel(instance()) } }
    bind { provider { LoginViewModel(instance()) } }
    bind { provider { MainViewModel(instance()) } }
    bind { provider { ProjectViewModel(instance()) } }
    bind { provider { TaskViewModel(instance()) } }
    bind { provider { OfflineViewModel(instance()) } }
    bind { provider { CadViewModel(instance()) } }
}

val dbModule = DI.Module("dbModule") {
    bind {
        singleton {
            AdmDatabase(instance())
        }
    }
}