import User from "../../FrameWork/User/User";
import Common5 from "../../Platform/th/Common5";
import GameBase from "../SCommon/GameBase";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import ChatDialog from "./ChatDialog";
import ChatManager from "./ChatManager";
import JuQingManager from "./JuQingManager";
import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
const {ccclass, property} = cc._decorator;

@ccclass
export default class ChatList extends GameBase {
    //聊天列表
    @property(cc.Node)
    chatListNode:cc.Node = null
    @property(cc.Node)
    chatListContent:cc.Node = null
    @property(cc.Node)
    listItem:cc.Node = null

    curDay = 0//当天
     curChatTag = ''
     isChange = true

    initView(chatTag){
        this.curChatTag = chatTag
        this.curDay = User.getDay()
        this.initListInfo()
    }

    onLoad() {
        super.onLoad()
      
    }

    protected start(): void {
        this.curDay = User.getDay()
        this.isChange = true
        this.initListInfo()

        EventMgr.onEvent_custom(ryw_Event.ExitBtnEvent,()=>{
           
            if(cc.isValid(this.node, true)){
                this.isChange = true
                console.log('initListInfo++++++++++++刷新')
                this.chatListContent.removeAllChildren()
                this.chatListContent.destroyAllChildren()
                this.initListInfo()
            }
           
        },this)
        console.log('isHaveNewMessage=========================', ChatManager.isHaveNewMessage());
        
        this.scheduleOnce(()=>{
            PrefabManage.preloadPrefabByType(GameType.ChatDialog)
        },0)
    }
    onDestroy(): void {
        super.onDestroy()
    }

    //初始化聊天列表(第几天)
    initListInfo(){
        console.log("当前是第"+this.curDay+"天--")
     
        let allJuQingChatConfig:any = Common5.allJuQingChatConfig    
  
        let nameArray = []
        let tempNameTab = []
        let tempChilds = []
        Object.keys(allJuQingChatConfig).forEach(name => {  
            //console.log(name)
            nameArray.push(name)
        });
        let count = nameArray.length;
        let i = 0
        this.schedule(() => {
            let name = nameArray[i]
            let personConfig = JuQingManager.getPersonJsonByJsonName(name)
            if(personConfig.type == 'RJB' || personConfig.type == 'DH'){
              
            }else{

                let personJson = ChatManager.getPersonConfig(name)
                let recordData = ChatManager.getChatRecordDataByJson(name)
                
                //console.log(recordData,'recordData+++++++++++++===')
                if(recordData && recordData.length > 0){
                    //duihuaArray.push[]                
                    let childItem = cc.instantiate(this.listItem)
                    let nameStr = ChatManager.getNameByJson(name)
                    childItem.getChildByName("nameStr").getComponent(cc.Label).string = nameStr
                    tempNameTab.push(nameStr)
                    let chatPerson = ChatManager.getConfigByJsonName(name)
                    
                    Common5.addUrlSprite_custom("head/"+chatPerson.imgPath, childItem.getChildByName("avatar").getComponent(cc.Sprite));
                    let itemInfo = this.getListItemStr(name,personJson,recordData,childItem.getChildByName("flag"))
                    let contentStr = itemInfo.contentDesc
                    let indexStr = itemInfo.indexStr
                    if(contentStr.length > 14){
                        contentStr = contentStr.substr(0, 14) + "..."
                    }
                    childItem.getChildByName("contentStr").getComponent(cc.Label).string = contentStr
                    childItem.attr({jsonName:name,playInfo:allJuQingChatConfig[name],indexStr:indexStr})
                    childItem.active = true
                    let isFinish = true
                    for(let rei = 0;rei<recordData.length;rei++){
                        let data_ = recordData[rei]
                        if(data_.isFinish == false){
                            isFinish = false
                            break
                        }
                    }
                    childItem['isFinish'] = isFinish
                    tempChilds[tempChilds.length] = childItem
                  
                }
            }
            i++
            
            if(i==count && this.isChange){
               //聊天置顶
               this.isChange = false
                if(tempChilds.length>0){
                    let idexz = 0
                    for(let i = 0;i<tempChilds.length;i++){

                        if(!tempChilds[i]['isFinish']){
                            idexz++
                            tempChilds[i].zIndex = idexz
                            this.chatListContent.addChild(tempChilds[i])
                        }
                    }

                    for(let i = 0;i<tempChilds.length;i++){
                        if(tempChilds[i]['isFinish']){
                            idexz++
                            tempChilds[i].zIndex = idexz
                            this.chatListContent.addChild(tempChilds[i])
                        }
                    }

                }


                if(Common5.isOpenChatPeople != ''){
                    let people = Common5.isOpenChatPeople;
                    let event = {
                        target:{
                            jsonName: people
                        }
                    }
                    Common5.isOpenChatPeople = ''
                    this.onItemListClick(event) 
                }
                PrefabManage.preloadPrefabByType(GameType.ChatDialog)
            }
        }, 0, count-1, 0)

       
    }
    //点击进入聊天
    onItemListClick(event){
        PrefabManage.loadPrefabByType(GameType.ChatDialog,null,(prefabNode)=>{
            prefabNode.getComponent(ChatDialog).enterDialog(event.target.jsonName,null,()=>{
                this.initListInfo()
            })
        })
    }
    
    //显示列表的显示内容
    getListItemStr(jsonName,personJson,recordData,flagNode){
        let indexStr = ''
        //优先显示未完成对话的第一条
        for(let i=0;i<recordData.length;i++){
            if(!recordData[i].isFinish){
                indexStr = recordData[i].indexStr
                for(let j=0;j<personJson.length;j++){
                    if(personJson[j].indexStr == indexStr){
                        flagNode.active = true
                        return {contentDesc:personJson[j].contentDesc,indexStr:indexStr}
                    }
                }
            }
        }
        //显示已聊天的最后一条
        indexStr = recordData[recordData.length-1].indexStr
        for(let k=personJson.length-1;k>=0;k--){
            if(personJson[k].indexStr == indexStr){
                flagNode.active = false
                return {contentDesc:personJson[k].contentDesc,indexStr:indexStr}
            }
        }
    
        PrefabManage.showTextTips('这里没获取到内容?'+jsonName)
        flagNode.active = false
        return {contentDesc:"",indexStr:""}
    }
}