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

import android.util.Log
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.MainFragmentContract
import com.wdkl.ncs.android.middleware.model.*
import com.wdkl.ncs.android.middleware.model.vo.CustomerInfoVO
import com.wdkl.ncs.android.middleware.model.vo.FrameAudioVO
import com.wdkl.ncs.android.middleware.model.vo.FramePartVO
import io.reactivex.disposables.Disposable
import javax.inject.Inject

class MainFragmentPresenter @Inject constructor() : RxPresenter<MainFragmentContract.View>(), MainFragmentContract.Presenter  {

    @Inject
    protected lateinit var boxDeviceApi : BoxDeviceApi

    /**
     * @Name  observer
     * @Type  ConnectionObserver
     * @Note  数据监听者
     */
    private val observer = object : ConnectionObserver<Any>() {
        override fun onStartWithConnection() {
            providerView().start()
        }

        override fun onNextWithConnection(result: Any, connectionQuality: ConnectionQuality) {
            providerView().complete("")
            when (result) {
                is CustomerInfoVO -> {
                    providerView().showCustomInfo(result)
                }

                is FramePartVO -> {
                    providerView().showFrames(result)
                }

                is FrameAudioVO -> {
                    providerView().showFrameDevices(result)
                }
            }
        }

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

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


        override fun onNoneNet() {
            Log.d("MainFragmentPresenter", "onNoneNet...")
            super.onNoneNet()
            providerView().onNoNet()
        }
    }

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

    override fun loadCustomInfo(customId: Int) {
        boxDeviceApi.getCustomInfo(customId)
                .map {
                    val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
                    val customerInfoVO = gson.fromJson(it.getJsonString(), CustomerInfoVO::class.java)

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

    override fun loadFrame(partId: Int) {
        boxDeviceApi.getFramePart(partId)
            .map {
                val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
                val frames = gson.fromJson(it.getJsonString(), FramePartVO::class.java)

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

    override fun loadFrameDevices(audioId: Int) {
        boxDeviceApi.getFrameDevice(audioId)
            .map {
                val gson = GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create()
                val frameVo = gson.fromJson(it.getJsonString(), FrameAudioVO::class.java)

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