package com.bc_chat.im.chat

import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.ListView
import com.bc_chat.bc_base.utils.Const
import com.bc_chat.im.R
import com.bc_chat.im.adapter.ConversationListAdapterEx
import com.zhaohaoting.framework.utils.observer.Observer
import com.zhaohaoting.framework.utils.observer.ObserverManager
import io.rong.imkit.RongIM
import io.rong.imkit.fragment.ConversationListFragment
import io.rong.imkit.fragment.IHistoryDataResultCallback
import io.rong.imkit.model.Event
import io.rong.imkit.widget.adapter.ConversationListAdapter
import io.rong.imlib.model.Conversation
import io.rong.imlib.model.Message
import org.jetbrains.anko.find


/**
 * author：Created by zhaohaoting on 2019/2/26
 * email：526309416@qq.com
 * desc：会话联系人
 */
class MConversationListFragment : ConversationListFragment(), Observer<String> {

    private lateinit var rcList: ListView

    //    private var headerView: View? = null
    private var conversationListAdapterEx: ConversationListAdapterEx? = null
    private var sysMessageConversation: Conversation = Conversation.obtain(Conversation.ConversationType.PRIVATE, Const.SERVICE_ID, Const.SERVICE_NAME)
    private var noticeMessageConversation: Conversation = Conversation.obtain(Conversation.ConversationType.SYSTEM, Const.SERVICE_ID, Const.SERVICE_NOTICE_NAME)


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        conversationListAdapterEx = ConversationListAdapterEx(context!!)

        ObserverManager.getInstance().registerObserver(Const.ObserverKey.GROUP_KICKING, this)

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        rcList = view.find(R.id.rc_list)
//        headerView = layoutInflater.inflate(R.layout.header_msgs_conversation, rcList, false)
//        if (!Const.SERVICE_ID.equals(RongIM.getInstance().currentUserId)) {
//            rcList.addHeaderView(headerView)
//        }
        //先隐藏融云有的emptyView
//        rcList.emptyView?.gone()
//        //再清除sdk 的emptyView
//        rcList.emptyView = null

    }

    /**
     * 存在还未登录成功就获取会话记录情况，故放在此处 初始和连接成功都会调用
     */
    override fun getConversationList(conversationTypes: Array<out Conversation.ConversationType>?, callback: IHistoryDataResultCallback<MutableList<Conversation>>?) {
        super.getConversationList(conversationTypes, callback)
//        refreshSysMsg()
//        refreshNoticeMsg()
    }

    /**
     * 接收消息回调
     */
    override fun onEventMainThread(event: Event.OnReceiveMessageEvent) {
        if (!ChatUtil.isGroupInvite(event.message)) {
            super.onEventMainThread(event)
        }
//        val message = event.message
//        if (message.conversationType == Conversation.ConversationType.PRIVATE && Const.SERVICE_ID.equals(message.targetId)) {
//            refreshSysMsg()
//        }
//        if (message.conversationType == Conversation.ConversationType.SYSTEM && Const.SERVICE_ID.equals(message.targetId)) {
//            refreshNoticeMsg()
//        }
    }

    /**
     * 发送消息回调
     */
    override fun onEventMainThread(message: Message) {
        super.onEventMainThread(message)
//        if (message.conversationType == Conversation.ConversationType.PRIVATE && Const.SERVICE_ID.equals(message.targetId)) {
//            refreshSysMsg()
//        } else if (message.conversationType == Conversation.ConversationType.SYSTEM && Const.SERVICE_ID.equals(message.targetId)) {
//            refreshNoticeMsg()
//        }
    }
//
//    /**
//     * 刷新系统消息
//     */
//    private fun refreshSysMsg() {
//        RongIMClient.getInstance().getConversation(Conversation.ConversationType.PRIVATE, Const.SERVICE_ID, object : RongIMClient.ResultCallback<Conversation>() {
//
//            override fun onError(p0: RongIMClient.ErrorCode?) {
//                val provider = RongContext.getInstance().getConversationTemplate(sysMessageConversation.conversationType?.getName())
//                val view = headerView?.sys_content?.inflate<IContainerItemProvider.ConversationProvider<*>>(provider)
//                val uIConversation = UIConversation.obtain(context, sysMessageConversation, false)
//                uIConversation.uiConversationTitle = Const.SERVICE_NAME
//                provider.bindView(view, 0, uIConversation)
//                headerView?.tv_sys_message_unread?.visibility = View.GONE
//            }
//
//            override fun onSuccess(p0: Conversation?) {
//                if (p0 != null) {
//                    sysMessageConversation = p0
//                }
//                val provider = RongContext.getInstance().getConversationTemplate(sysMessageConversation.conversationType?.getName())
//                val view = headerView?.sys_content?.inflate<IContainerItemProvider.ConversationProvider<*>>(provider)
//                val uIConversation = UIConversation.obtain(context, sysMessageConversation, false)
//                uIConversation.uiConversationTitle = Const.SERVICE_NAME
//                provider.bindView(view, 0, uIConversation)
//                if (sysMessageConversation.unreadMessageCount > 0) {
//                    if (sysMessageConversation.unreadMessageCount > 99) {
//                        headerView?.tv_sys_message_unread?.text = "99+"
//                    } else {
//                        headerView?.tv_sys_message_unread?.text = sysMessageConversation.unreadMessageCount.toString()
//                    }
//                    headerView?.tv_sys_message_unread?.visibility = View.VISIBLE
//                } else {
//                    headerView?.tv_sys_message_unread?.visibility = View.GONE
//                }
//            }
//        })
//    }
//
//    /**
//     * 刷新同通知消息
//     */
//    private fun refreshNoticeMsg() {
//        RongIMClient.getInstance().getConversation(Conversation.ConversationType.SYSTEM, Const.SERVICE_ID, object : RongIMClient.ResultCallback<Conversation>() {
//
//            override fun onError(p0: RongIMClient.ErrorCode?) {
//                val provider = RongContext.getInstance().getConversationTemplate(noticeMessageConversation.conversationType?.getName())
//                val view = headerView?.notice_content?.inflate<IContainerItemProvider.ConversationProvider<*>>(provider)
//                val uIConversation = UIConversation.obtain(context, noticeMessageConversation, false)
//                uIConversation.uiConversationTitle = Const.SERVICE_NOTICE_NAME
//                provider.bindView(view, 0, uIConversation)
//                headerView?.tv_sys_notice_unread?.visibility = View.GONE
//            }
//
//            override fun onSuccess(p0: Conversation?) {
//                if (p0 != null) {
//                    noticeMessageConversation = p0
//                }
//                val provider = RongContext.getInstance().getConversationTemplate(sysMessageConversation.conversationType?.getName())
//                val view = headerView?.notice_content?.inflate<IContainerItemProvider.ConversationProvider<*>>(provider)
//                val uIConversation = UIConversation.obtain(context, noticeMessageConversation, false)
//                uIConversation.uiConversationTitle = Const.SERVICE_NOTICE_NAME
//                provider.bindView(view, 0, uIConversation)
//                if (noticeMessageConversation.unreadMessageCount > 0) {
//                    if (noticeMessageConversation.unreadMessageCount > 99) {
//                        headerView?.tv_sys_notice_unread?.text = "99+"
//                    } else {
//                        headerView?.tv_sys_notice_unread?.text = noticeMessageConversation.unreadMessageCount.toString()
//                    }
//                    headerView?.tv_sys_notice_unread?.visibility = View.VISIBLE
//                } else {
//                    headerView?.tv_sys_notice_unread?.visibility = View.GONE
//                }
//            }
//        })
//    }
//
//    override fun onUnreadCountChanged() {
//        refreshSysMsg()
//        refreshNoticeMsg()
//    }

    override fun onResolveAdapter(context: Context): ConversationListAdapter {
        return conversationListAdapterEx!!
    }

    override fun onDestroy() {
        super.onDestroy()
        ObserverManager.getInstance().unregisterObserver(Const.ObserverKey.GROUP_KICKING, this)
    }

    override fun onEvent(key: String?, var1: String?) {
        when (key) {
            Const.ObserverKey.GROUP_KICKING -> {
                var uiConversation = ChatUtil.getUIConversation(conversationListAdapterEx, var1);
                if (uiConversation != null) {
                    RongIM.getInstance().removeConversation(uiConversation.conversationType, uiConversation.conversationTargetId)
                }

            }
        }
    }

}