package com.gingersoft.gsa.other_order_mode.data

import com.gingersoft.gsa.other_order_mode.data.network.CoolWeatherNetwork
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.FormBody

class WeatherRepository private constructor(private val network: CoolWeatherNetwork) {


    suspend fun requestOrderList(status: String, type: String, page: String, orderNo: String, phone: String) = withContext(Dispatchers.IO) {
        val requestBody = FormBody.Builder()
                .add("restaurantId", "26")
                .add("page", page)
                .add("status", status)
                .add("orderNo", orderNo)
                .add("phone", phone)
                .add("type", type)
                .build()
        val heWeather = network.fetchOrderList(requestBody)
        heWeather
    }

    suspend fun updateRestOpenStatus(state: Boolean) = withContext(Dispatchers.IO) {
        val requestBody = FormBody.Builder()
                .add("restId", "26")
                .add("openStatus", if (state) "1" else "2")
                .build()
        val data = network.updateRestOpenStatus(requestBody)
        data
    }


    suspend fun getOrderInfo(orderId: String) = withContext(Dispatchers.IO) {
        val requestBody = FormBody.Builder()
                .add("orderId", orderId)
                .build()
        val data = network.getOrderInfo(requestBody)
        data
    }



    companion object {
        private lateinit var instance: WeatherRepository

        fun getInstance(network: CoolWeatherNetwork): WeatherRepository {
            if (!::instance.isInitialized) {
                synchronized(WeatherRepository::class.java) {
                    if (!::instance.isInitialized) {
                        instance = WeatherRepository(network)
                    }
                }
            }
            return instance
        }

    }

}