package com.wdkl.ncs.android.middleware.logic.presenter.callingbed

import com.enation.javashop.net.engine.plugin.connection.ConnectionQuality
import com.enation.javashop.net.engine.plugin.exception.ExceptionHandle
import com.enation.javashop.net.engine.utils.ThreadFromUtils
import com.google.gson.FieldNamingPolicy
import com.google.gson.GsonBuilder
import com.wdkl.ncs.android.lib.base.RxPresenter
import com.wdkl.ncs.android.lib.utils.ConnectionObserver
import com.wdkl.ncs.android.lib.utils.getJsonString
import com.wdkl.ncs.android.middleware.api.CallingbedApi
import com.wdkl.ncs.android.middleware.di.MiddlewareDaggerComponent
import com.wdkl.ncs.android.middleware.logic.contract.callingbed.CallingbedActivityContract
import com.wdkl.ncs.android.middleware.model.ServerInfo
import com.wdkl.ncs.android.middleware.model.dos.AppVersionDO
import com.wdkl.ncs.android.middleware.model.dos.DeviceDO
import com.wdkl.ncs.android.middleware.model.dos.PartSettingDO
import com.wdkl.ncs.android.middleware.model.dto.TcpSeverDTO
import com.wdkl.ncs.android.middleware.model.vo.BedDeviceInfoVO
import com.wdkl.ncs.android.middleware.model.vo.CustomerInfoVO
import io.reactivex.disposables.Disposable
import javax.inject.Inject

class CallingbedActivityPresenter @Inject constructor() :RxPresenter<CallingbedActivityContract.View>(), CallingbedActivityContract.Presenter {

    override fun bindDagger() {
        MiddlewareDaggerComponent.component.inject(this)
    }

    @Inject
    protected lateinit var bedDeviceApi: CallingbedApi

    private val observer = object :ConnectionObserver<Any>() {
        override fun onStartWithConnection() {
            providerView().start()
        }

        override fun onNextWithConnection(result: Any, connectionQuality: ConnectionQuality) {
            when (result) {
                is BedDeviceInfoVO -> {
                    //设备信息
                    providerView().showDeviceInfo(result)
                }

                is PartSettingDO -> {
                    //设置参数
                    providerView().setPartSettings(result)
                }

                is TcpSeverDTO -> {
                    //tcp服务器地址
                    providerView().setTcpServerHost(result)
                }

                is ServerInfo -> {
                    providerView().setServerInfo(result)
                }

                is AppVersionDO -> {
                    //app版本信息
                    providerView().loadAppVersion(result)
                }

                is DeviceDO -> {
                    providerView().showEmergencyInfo(result)
                }
            }
        }

        override fun onErrorWithConnection(error: ExceptionHandle.ResponeThrowable, connectionQuality: ConnectionQuality) {
            providerView().onError(error.customMessage)
        }

        override fun attachSubscribe(var1: Disposable) {
            addDisposable(var1)
        }

        override fun onNoneNet() {
            super.onNoneNet()
            providerView().onNoNet()
        }
    }

    override fun loadDeviceInfo(mac: String) {
        bedDeviceApi.getDeviceInfo(mac)
                .map {
                    val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
                    val deviceInfo = gson.fromJson(it.getJsonString(), BedDeviceInfoVO::class.java)

                    return@map deviceInfo
                }
                .compose(ThreadFromUtils.defaultSchedulers())
                .subscribe(observer)
    }

    override fun loadPartSettings(partId: Int) {
        bedDeviceApi.getPartSettings(partId)
                .map {
                    val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
                    val settings = gson.fromJson(it.getJsonString(), PartSettingDO::class.java)

                    return@map settings
                }
                .compose(ThreadFromUtils.defaultSchedulers())
                .subscribe(observer)
    }

    override fun loadTcpServerHost() {
        bedDeviceApi.getTcpServerHost()
                .map {
                    val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
                    val tcpServer = gson.fromJson(it.getJsonString(), TcpSeverDTO::class.java)

                    return@map tcpServer
                }
                .compose(ThreadFromUtils.defaultSchedulers())
                .subscribe(observer)
    }

    override fun loadServerInfo() {
        bedDeviceApi.getServerInfo()
            .map {
                val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
                val tcpServer = gson.fromJson(it.getJsonString(), ServerInfo::class.java)

                return@map tcpServer
            }
            .compose(ThreadFromUtils.defaultSchedulers())
            .subscribe(observer)
    }

    override fun getAppVersion(partId: Int, deviceType: Int) {
        bedDeviceApi.getAppVersion(partId, deviceType)
            .map {
                val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
                val appVersion = gson.fromJson(it.getJsonString(), AppVersionDO::class.java)

                return@map appVersion
            }
            .compose(ThreadFromUtils.defaultSchedulers())
            .subscribe(observer)
    }

    override fun getEmergencyDeviceInfo(frameId: Int) {
        bedDeviceApi.getEmergencyInfo(frameId)
            .map {
                val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
                val info = gson.fromJson(it.getJsonString(), DeviceDO::class.java)

                return@map info
            }
            .compose(ThreadFromUtils.defaultSchedulers())
            .subscribe(observer)
    }
}