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

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.*
import com.wdkl.ncs.android.middleware.api.BoxDeviceApi
import com.wdkl.ncs.android.middleware.di.MiddlewareDaggerComponent
import com.wdkl.ncs.android.middleware.logic.contract.conversion_box.MainActivityContract
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.PartSettingDO
import com.wdkl.ncs.android.middleware.model.dto.TcpSeverDTO
import com.wdkl.ncs.android.middleware.model.vo.DeviceNurseInfoVO
import io.reactivex.disposables.Disposable
import javax.inject.Inject

class MainActivityPresenter @Inject constructor() :RxPresenter<MainActivityContract.View>(), MainActivityContract.Presenter {
    override fun bindDagger() {
        MiddlewareDaggerComponent.component.inject(this)
    }

    @Inject
    protected lateinit var boxDeviceApi: BoxDeviceApi

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

        override fun onNextWithConnection(result: Any, connectionQuality: ConnectionQuality) {
            when (result) {
                is DeviceNurseInfoVO -> {
                    //设备信息
                    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)
                }
            }
        }

        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) {
        boxDeviceApi.getDeviceInfo(mac)
                .map {
                    val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
                    val deviceInfo = gson.fromJson(it.getJsonString(), DeviceNurseInfoVO::class.java)

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

    override fun loadPartSettings(partId: Int) {
        boxDeviceApi.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() {
        boxDeviceApi.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() {
        boxDeviceApi.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) {
        boxDeviceApi.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 setDeviceMac(device_mac: String, trans_audio_mac: String) {
        boxDeviceApi.setDeviceMac(device_mac, trans_audio_mac)
            .compose(ThreadFromUtils.defaultSchedulers())
            .subscribe(observer)
    }

}