package com.yidianling.im.router

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.util.Log
import com.netease.nimlib.sdk.NIMClient
import com.netease.nimlib.sdk.RequestCallback
import com.netease.nimlib.sdk.auth.LoginInfo
import com.netease.nimlib.sdk.msg.MsgService
import com.netease.nimlib.sdk.uinfo.UserService
import com.netease.nimlib.sdk.uinfo.constant.UserInfoFieldEnum
import com.ydl.ydl_router.manager.YDLRouterManager
import com.ydl.ydlcommon.base.config.HttpConfig
import com.ydl.ydlcommon.bean.GlobalInfo
import com.ydl.ydlcommon.data.PlatformDataManager
import com.ydl.ydlcommon.modular.ModularServiceManager
import com.ydl.ydlcommon.router.IYDLRouterConstant
import com.yidianling.im.config.constants.UserPreferences
import com.yidianling.im.modular.service.IMServiceImpl
import com.yidianling.im.preference.IMCache
import com.yidianling.router.RouterManager
import com.yidianling.router.app.ReceiveRedPacketParam
import com.yidianling.uikit.api.NimUIKit
import com.yidianling.user.api.bean.UserResponseBean
import com.yidianling.user.api.service.IUserService

/**
 * 功能输入接口
 * 暴漏给app模块使用
 * Created by hgw on 2018/3/19.
 */
object ImIn {

    fun getImService(): IMServiceImpl {
        return ModularServiceManager.provide(IMServiceImpl::class.java)
    }

    fun getUserService(): IUserService {
        return ModularServiceManager.provide(IUserService::class.java)
    }

    /**
     * 获取专家主页url
     */
    fun getExpertHost(): String {
        return  HttpConfig.H5_URL + "experts/"
    }

    fun getShareExpertHost(): String {
        return  HttpConfig.MH5_URL+ "experts/"
    }

    fun getReserveHost(): String {
        return  HttpConfig.H5_URL+"booking/order-detail?orderid="
    }

    fun closeMeidplayer(){
        RouterManager.getCourseRouter()?.closePlayer()
        RouterManager.getFMRouter()?.closePlayer()
    }
    /**
     * 更新用户设置
     */
    fun setChatTeamHisShowed(){
        getUserService()?.setChatTeamHisShowed(true)
    }

    /**
     * 是否登录
     */
    fun isLogin(activity: Context,flag : Boolean):Boolean{
        if (!isLogin() && flag) {
              YDLRouterManager.router(IYDLRouterConstant.ROUTER_MINE_LOGIN)
            return false
        }else{
            return true
        }
    }

    fun membersIntent(activity: Activity, userId: String): Intent? {
        // TODO: 2019-12-12 待替换 by:HaoRui
        return RouterManager.getDynamicRouter()?.membersIntent(activity, userId)
    }

    fun replayInfoIntent(activity: Activity, replyId: String, aid: String): Intent? {
        // TODO: 2019-12-12 待替换 by:HaoRui
        return RouterManager.getDynamicRouter()?.replyInfoIntent(activity, replyId, aid)
    }


    fun myRedPockIntent(activity: Activity): Intent? {
        return RouterManager.getAppRouter()?.myRedPockIntent(activity)
    }

    fun trendsDetailIntent(activity: Activity, trendId: Int): Intent? {
        // TODO: 2019-12-12 待替换 by:HaoRui
        return RouterManager.getDynamicRouter()?.trendsDetailIntent(activity, trendId)
    }

    fun trendsDetailIntent(activity: Activity, trendId: Int, isScrollToZan: Boolean): Intent? {
        // TODO: 2019-12-12 待替换 by:HaoRui
        return RouterManager.getDynamicRouter()?.trendsDetailIntent(activity, trendId, isScrollToZan)
    }

    fun trendsDetailIntent(activity: Activity, trendId: Int, isScrollToZan: Boolean, lastId: Int): Intent? {
        // TODO: 2019-12-12 待替换 by:HaoRui
        return RouterManager.getDynamicRouter()?.trendsDetailIntent(activity, trendId, isScrollToZan, lastId)
    }

    /**
     * 跳转话题详情
     */
    fun topicDetailActivity(context: Context,topic_id : String){
        var intent = RouterManager.getDynamicRouter()?.topicDetailIntent(context, false,topic_id)
        intent?.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
    }

    /**
     * 课程专题页面
     */
    fun courseTopic(activity: Activity,id : String){
        RouterManager.getCourseRouter()?.courseTopic(activity,id)
    }

    fun fmDetailIntent(activity: Activity, id: Int): Intent? {
        return RouterManager.getFMRouter()?.fmDetailIntent(activity, id)
    }

    fun sendRedPacketIntent(activity: Activity, toUid: String): Intent? {
        return RouterManager.getAppRouter()?.sendRedPacketIntent(activity, toUid)
    }

    fun testResultH5(testResultId: Int){
        RouterManager.getTestsRouter()!!.testH5Result(testResultId.toString())
    }

    fun testDetailH5(testId: Int){
        RouterManager.getTestsRouter()?.testDetailH5(testId.toString());
    }

    fun receiveRedPacketIntent(activity: Activity, param: ReceiveRedPacketParam): Intent? {
        return RouterManager.getAppRouter()?.receiverRedPacketIntent(activity, param)
    }


    fun inputPhoneIntent(activity: Activity, smsAction: String): Intent? {
        return getUserService()?.inputPhoneIntent(activity, smsAction)
    }

    /**
     * 获取私聊未读数
     */
    fun getInUnreadNum():Int{
        return NIMClient.getService(MsgService::class.java).getTotalUnreadCount()
    }

    /**
     * 登录im
     */
    fun loginIm(uid : String,pwd : String){
        var info = LoginInfo(uid, pwd);
        var callback :  RequestCallback<LoginInfo>  = object : RequestCallback<LoginInfo> {
            override fun onSuccess(param: LoginInfo?) {
                Log.d("", "")
            }

            override fun onFailed(code: Int) {
            }

            override fun onException(exception: Throwable?) {
            }
        }
        IMCache.setAccount(uid)
        //此代码进行登录,并且会进行回调
        NimUIKit.login(info, callback);
    }

    fun loginIm(uid : String,pwd : String,callback: RequestCallback<LoginInfo>){
        var info = LoginInfo(uid, pwd);
        IMCache.setAccount(uid)
        //此代码进行登录,并且会进行回调
        NimUIKit.login(info, callback);
    }

    /**
     * 更新听筒模式
     */
    fun updateEarMode(boo : Boolean){
        NimUIKit.setEarPhoneModeEnable(boo)
    }

    /**
     * 获取当前听筒模式是否开启
     */
    fun getEarModeIsOpen() : Boolean{
        return NimUIKit.isEarPhoneModeEnable()
    }

    /**
     * 更新用户头像
     */
    fun updateUserHead(head : String){
        var fields = HashMap<UserInfoFieldEnum,Any>(1)
        fields.put(UserInfoFieldEnum.AVATAR, head)
        NIMClient.getService(UserService::class.java).updateUserInfo(fields)
    }

    /**
     * 更新用户昵称
     */
    fun updateUserName(name : String){
        var fields = HashMap<UserInfoFieldEnum,Any>(1)
        fields.put(UserInfoFieldEnum.Name, name)
        NIMClient.getService(UserService::class.java).updateUserInfo(fields)
    }

    /**
     * 设置通知声音开关
     */
    fun setRing(boo: Boolean){
        val config = UserPreferences.getStatusConfig()
        config.ring = boo
        UserPreferences.setStatusConfig(config)
        NIMClient.updateStatusBarNotificationConfig(config)
    }

    /**
     * 设置震动开关
     */
    fun setVibrate(boo: Boolean){
        val config = UserPreferences.getStatusConfig()
        config.vibrate = boo
        UserPreferences.setStatusConfig(config)
        NIMClient.updateStatusBarNotificationConfig(config)
    }

    fun isLogin(): Boolean {
        return getUserService()?.isLogin()?:false
    }

    fun getUserInfo(): UserResponseBean.UserInfo? {
        return getUserService().getUserInfo()
    }

    fun getUserResponse():UserResponseBean?{
        return getUserService().getUserResponse()
    }

    fun getChatTeamHisShow(): Boolean {
        return getUserService().getChatTeamHisShow()?:false
    }

    fun loginWayIntent(context: Context): Intent? {
        return getUserService().loginWayIntent(context)
    }

/*    fun mainIntent(context: Context): Intent? {
        return RouterManager.getAppRouter()?.mainIntent(context)
    }*/

    fun mainIntent(context: Context, selectTab: Int): Intent? {
        return RouterManager.getAppRouter()?.mainIntent(context, selectTab, false)
    }

    fun feedBackIntent(activity: Activity): Intent? {
        return RouterManager.getAppRouter()?.feedBackIntent(activity)
    }

    fun getGlobalInfo(): GlobalInfo? {
        return PlatformDataManager.getRam().getGlobalInfo()
    }
}
