import { _decorator, Component, Node } from 'cc';
import { uiManager } from '../../manager/uiManager';
import { home_car } from './home_car';
import { home_top } from './home_top';
import { home_bottom } from './home_bottom';
import { tools } from '../../tools';
import { ClientEvent } from '../../lib/clientEvent';
import { config } from '../../config';
import { userDataManager } from '../../manager/userDataManager';
import { user_results } from '../../data';
import { GameManager } from '../../GameManager';
import { home_modules } from './home_modules';
import { SdkUtil } from '../../sdkUtil';
const { ccclass, property } = _decorator;

@ccclass('home')
export class home extends Component {
    @property(Node) home_car:Node = null
    @property(Node) top:Node = null
    @property(Node) bottom:Node = null
    @property(Node) modules:Node = null
    @property(Node) btn_start_game:Node = null

    protected start(): void {
        uiManager.Instance().onButtonListen(this.btn_start_game,()=>{
            GameManager.checkPlayGame(this.node,()=>{
                tools.playGame(()=>{
                    this.node.parent.active = true
                    ClientEvent.dispatchEvent(config.UI_EVENT.HOME_DID_BECOME_ACTIVE)
                })
                this.node.parent.active = false
                ClientEvent.dispatchEvent(config.UI_EVENT.HOME_DID_BECOME_INACTIVE)
            })
        })

        ClientEvent.on(config.UI_EVENT.HOME_DID_BECOME_ACTIVE,this.homeDidBecomeActive.bind(this),this)
        ClientEvent.on(config.UI_EVENT.HOME_DID_BECOME_INACTIVE,this.homeDidBecomeInactive.bind(this),this)
        ClientEvent.on(config.UI_EVENT.UPDATE_GAME_FREE_COUNT,this.onUpdateGameFreeCount.bind(this),this)
        ClientEvent.on(config.UI_EVENT.GAME_OVER_SETTLE_ACCOUNT,this.onGameOverSettleAccount.bind(this),this)
        ClientEvent.on(config.UI_EVENT.GET_NEW_CAR,this.onGetNewCar.bind(this),this)
        ClientEvent.on(config.UI_EVENT.UPDATE_USER_DEFAULT_CAR,this.onUpdateUserDefaultCar.bind(this),this)
        ClientEvent.on(config.UI_EVENT.UPDATE_USER_INFO,this.onUpdateUserInfo.bind(this),this)
        ClientEvent.on(config.UI_EVENT.UPDATE_USER_AVATAR_BORDER,this.onUpdateUserAvatarBorder.bind(this),this)
        ClientEvent.on(config.UI_EVENT.UPDATE_RED_DOT_STATUS,this.onUpdateRedDotStatus.bind(this),this)
        ClientEvent.on(config.EVENT_TYPE.MSG_DATA,this.onMsgData.bind(this),this)

        SdkUtil.showGridGamePanel()
    }

    public init(){
        this.top.getComponent(home_top).init()
        this.home_car.getComponent(home_car).updateCar()
        this.bottom.getComponent(home_bottom).init()
        this.modules.getComponent(home_modules).init()
    }

    homeDidBecomeActive() {
        // console.log('home 活跃的 活跃的 活跃的')
        SdkUtil.showGridGamePanel()
    }

    homeDidBecomeInactive() {
        // console.log('home 不活跃的 不活跃的 不活跃的')
        this.modules.getComponent(home_modules).hideGengduoView()
        SdkUtil.hideGridGamePanel()
    }

    onUpdateGameFreeCount() {
        this.bottom.getComponent(home_bottom).reloadGameFreeCount()
    }
    
    onGameOverSettleAccount(res:user_results){
        // 更新本周分数、荣誉
        tools.mine_rank_data.score = res.max_integral
        this.bottom.getComponent(home_bottom).reloadWeekScore()
        tools.mine_rank_data.city_badge_status = res.city_badge_status
        tools.mine_rank_data.province_badge_status = res.province_badge_status
        tools.mine_rank_data.nationwide_badge_status = res.nationwide_badge_status
        this.bottom.getComponent(home_bottom).reloadHonorData()
    }

    onGetNewCar(res:user_results){
        // 更新默认车
        // userDataManager.user_car_list.default_car_id = res.default_car_id
        // console.log("获取新车:",dataManager.getUserCarInfo(userDataManager.user_car_list.default_car_id))
        // 更新用户解锁车列表
        userDataManager.user_car_list.car_list = res.unlock_car_list
        // 更新首页车
        this.home_car.getComponent(home_car).updateCar()
        // 检查我在排行中,更换车数据
        this.bottom.getComponent(home_bottom).checkIsMineCountryRank()
    }

    onUpdateUserDefaultCar() {
        // 更新首页车
        this.home_car.getComponent(home_car).updateCar()
        // 检查我在排行中,更换车数据
        this.bottom.getComponent(home_bottom).checkIsMineCountryRank()
    }

    onUpdateUserInfo() {
        this.top.getComponent(home_top).reloadUserInfo()
    }

    onUpdateUserAvatarBorder() {
        this.top.getComponent(home_top).reloadUserAvatarBorder()
    }

    onUpdateRedDotStatus(type:number) {
        switch (type) {
            case config.RED_DOT_TYPE.announcement:
                this.updateAnnouncementRedDotStatus()
                break;
            case config.RED_DOT_TYPE.sign:
                this.updateSignRedDotStatus()
                break;
            case config.RED_DOT_TYPE.mail:
                this.updateMailRedDotStatus()
                break;
            default:
                break;
        }
    }

    onMsgData(data) {
        let msg = data.msg
        switch (data.action) {
            case config.MSG_TYPE.guangbo:
                this.top.getComponent(home_top).reloadGuangboData(msg.notice_list)
                this.bottom.getComponent(home_bottom).reloadCountryRankData(msg.ranking_list)
                if(msg.ranking_list.length>0) {
                    for (let index = 0; index < msg.ranking_list.length; index++) {
                        const element = msg.ranking_list[index]
                        if(element.user_id==userDataManager.user_data.id) {
                            tools.mine_rank_data.nationwide_badge_status = 1
                            this.bottom.getComponent(home_bottom).reloadHonorData()
                            break
                        }
                    }
                }
                break;
            case config.MSG_TYPE.announcement:
                userDataManager.user_red_dot_data.bulletin_red_dot = msg.bulletin_red_dot
                this.updateAnnouncementRedDotStatus()
                break
            case config.MSG_TYPE.mail:
                userDataManager.user_red_dot_data.mail_unread_number = msg.mail_unread_number
                this.updateMailRedDotStatus()
                break
            default:
                break;
        }   
    }

    private updateAnnouncementRedDotStatus() {
        if(userDataManager.user_red_dot_data.bulletin_red_dot==1) {
            this.modules.getComponent(home_modules).showRedDotGonggao(true)
        } else {
            this.modules.getComponent(home_modules).showRedDotGonggao(false)
        }
    }

    private updateSignRedDotStatus() {
        if(userDataManager.user_red_dot_data.sign_red_dot==1) {
            this.modules.getComponent(home_modules).showRedDotQiandao(true)
        } else {
            this.modules.getComponent(home_modules).showRedDotQiandao(false)
        }
    }

    private updateMailRedDotStatus() {
        if(userDataManager.user_red_dot_data.mail_unread_number>0) {
            this.modules.getComponent(home_modules).showRedDotYoujian(true)
        } else {
            this.modules.getComponent(home_modules).showRedDotYoujian(false)
        }
    }
}


