package com.orange.net.net

import android.text.TextUtils
import com.orange.myapplication.data.work.data.WeatherData
import com.orange.net.util.GsonConverterFactory
import okhttp3.OkHttpClient
import retrofit2.Retrofit
//import retrofit2.converter.gson.GsonConverterFactory
import java.lang.NullPointerException

class NetWorkEngine : Api {

    private lateinit var retrofit:Retrofit
    private lateinit var reqUrl:String
    private var api: Api? = null
    get() {
        if(!::reqUrl.isInitialized){
            throw NullPointerException("url hasn't init , invoke NetWorkEngine#init()")
        }
        return field
    }

    fun init(reqUrl:String){
        reqUrl.let {
            this@NetWorkEngine.reqUrl = it
            initRetrofit(it)
        }
    }

    private fun initRetrofit(reqUrl: String){
        retrofit = Retrofit.Builder()
            .baseUrl(reqUrl)
            .client(provideClient())
//            .addCallAdapterFactory()
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        api = retrofit.create(Api::class.java)
    }

    private fun provideClient():OkHttpClient{
        return OkHttpClient.Builder()
            .build()
    }



    override suspend fun getWeatherInfo(id:String): WeatherData {
        return api!!.getWeatherInfo(id)
    }
}