package com.miya.fastcashier.service

import android.util.Log
import com.miya.fastcashier.beans.SelfCashierAccountInfo
import com.miya.fastcashier.beans.LoginRequest;
import com.miya.fastcashier.net.BaseResult
import com.miya.fastcashier.net.MiyaHttpLoggingInterceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.POST


interface LoginService {

    @POST("verify/auth/token")
    suspend fun login(@Body loginRequestCall: LoginRequest): BaseResult<SelfCashierAccountInfo>

    companion object {

        private const val BASE_URL = "https://hhms.miyapay.com/"

        private var service: LoginService? = null

        fun getApi(): LoginService {
            if (null == service) {
                val httpLoggingInterceptor =
                    MiyaHttpLoggingInterceptor {
                        Log.e("####", it)
                    }.apply { level = MiyaHttpLoggingInterceptor.Level.BODY }

                val client = OkHttpClient.Builder()
                    .addInterceptor(httpLoggingInterceptor)
                    .build()

                val retrofit = Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()

                service = retrofit.create(LoginService::class.java)
            }

            return service!!
        }

    }

}