import { _decorator, Component, Label, Node, Animation } from 'cc';
import { uiManager } from '../../manager/uiManager';
import { config } from '../../config';
import { user_info_view } from '../user_info_view';
import { home_bottom_rank_item } from './home_bottom_rank_item';
import { GameManager } from '../../GameManager';
import { tools } from '../../tools';
import { home_honor } from './home_honor';
import { home_bottom_countdown } from './home_bottom_countdown';
import { unLock_view } from '../unLock_view';
import { userDataManager } from '../../manager/userDataManager';
import { home_tt_sidebar } from './home_tt_sidebar';
import { SdkUtil } from '../../sdkUtil';
import { ClientEvent } from '../../lib/clientEvent';
import { rank } from '../rank/rank';
const { ccclass, property } = _decorator;

@ccclass('home_bottom')
export class home_bottom extends Component {
    @property(Node) img_honor:Node = null
    @property(Node) btn_wenhao:Node = null
    @property(Node) btn_gengduo:Node = null
    @property(Node) rank1_node:Node = null
    @property(Node) rank2_node:Node = null
    @property(Node) rank3_node:Node = null
    @property(Node) lab_week_score:Node = null
    @property(Node) img_loudou:Node = null
    @property(Node) btn_wuxiancishu:Node = null
    @property(Node) countdown_node:Node = null
    @property(Node) tt_btn_gift:Node = null
    @property(Node) tt_sidebar:Node = null
    private m_d_content = null
    private rank_item_list:home_bottom_rank_item[] = []

    start() {
        uiManager.Instance().onButtonListen(this.btn_wenhao, ()=>{
            uiManager.Instance().showUi(config.UI.ui_gameplay_view)
        })
        uiManager.Instance().onButtonListen(this.btn_gengduo, ()=>{
            this.node.parent.active = false
            uiManager.Instance().showUi(config.UI.rank, null, (node:Node)=>{
                node.getComponent(rank).onReloadCountryDataCallback((d_content)=>{
                    this.reloadCountryRankData(d_content)
                })
                this.node.parent.active = true
            })
        })
        uiManager.Instance().onButtonListen(this.btn_wuxiancishu, ()=>{
            if(userDataManager.getUserIsFreeAds()) {
                return
            }
            uiManager.Instance().showUi(config.UI.ui_unLock_view, null, (node:Node)=>{
                node.getComponent(unLock_view).initView((v:unLock_view)=>{
                    this.reloadCountdown()
                })
            })
        })
        uiManager.Instance().onButtonListen(this.tt_btn_gift, ()=>{
            this.onClickTTGift()
        })

        this.initRankItem()
    }

    private initRankItem() {
        let rank_1_com = this.rank1_node.getComponent(home_bottom_rank_item)
        let rank_2_com = this.rank2_node.getComponent(home_bottom_rank_item)
        let rank_3_com = this.rank3_node.getComponent(home_bottom_rank_item)

        this.rank_item_list.push(rank_1_com)
        this.rank_item_list.push(rank_2_com)
        this.rank_item_list.push(rank_3_com)

        rank_1_com.startAnimation(200)
        rank_2_com.startAnimation(250)
        rank_3_com.startAnimation(250)
    }

    public init() {
        this.reloadWeekScore()
        this.reloadHonorData()
        this.reloadCountdown()
        this.requestCountryRankData()
        this.reloadTTSidebar()
    }

    public reloadWeekScore() {
        this.lab_week_score.getComponent(Label).string = tools.mine_rank_data.score + ''
    }

    public reloadHonorData() {
        this.img_honor.getComponent(home_honor).initView()
    }

    private reloadCountdown() {
        this.countdown_node.getComponent(home_bottom_countdown).startTime((is_active)=>{
            this.btn_wuxiancishu.active = !is_active
            if(is_active) {
                this.img_loudou.getComponent(Animation).play()
            } else {
                this.img_loudou.getComponent(Animation).stop()
            }
        })
    }
    
    public requestCountryRankData(){
        GameManager.requestRankList(0, (d_content)=>{
            this.reloadCountryRankData(d_content)
        })
    }

    public reloadCountryRankData(d_content) {
        this.m_d_content = d_content
        if(d_content.length>0) {
            let rank_data = d_content[0]
            this.loadRankItemData(0,rank_data)
        }
        if(d_content.length>1) {
            let rank_data = d_content[1]
            this.loadRankItemData(1,rank_data)
        }
        if(d_content.length>2) {
            let rank_data = d_content[2]
            this.loadRankItemData(2,rank_data)
        }
    }

    public checkIsMineCountryRank() {
        if(this.m_d_content==undefined||this.m_d_content==null) {
            return
        }
        for (let index = 0; index < this.m_d_content.length; index++) {
            const element = this.m_d_content[index]
            if(element.user_id==userDataManager.user_data.id) {
                this.loadRankItemData(index,element)
                break
            }
        }
    }

    private loadRankItemData(index:number, rank_data) {
        this.rank_item_list[index].initView(rank_data, this.onClickRankGotoUserInfo.bind(this))
    }

    private onClickRankGotoUserInfo(item:home_bottom_rank_item) {
        let data = item.getData()
        if(data == null) {
            return
        }
        uiManager.Instance().showUi(config.UI.ui_user_info_view, null, (node:Node)=>{
            node.getComponent(user_info_view).initView(data)
        })
    }

    private reloadTTSidebar() {
        this.tt_btn_gift.active = false
        if(SdkUtil.ttCheckSceneShowRewards()==false || userDataManager.user_data.tt_sidebar_reward_status==1) {
            return
        }
        this.tt_btn_gift.active = true
        this.tt_btn_gift.getComponent(Animation).play()
    }

    private onClickTTGift() {
        let isToEnterFromSidebar = SdkUtil.ttCheckToEnterFromSidebar()
        this.tt_sidebar.getComponent(home_tt_sidebar).show(isToEnterFromSidebar, (r:home_tt_sidebar)=>{
            SdkUtil.ttNavToSidebarScene()
            r.close()
        },(r:home_tt_sidebar)=>{
            GameManager.requestTTSidebarUserReward(config.USER_TT_SIDEBAR_REWARD.SYNC, (d_content)=>{
                userDataManager.user_data.tt_sidebar_reward_status = d_content.status
                this.tt_btn_gift.active = false
                this.tt_btn_gift.getComponent(Animation).stop()
                this.ttSidebarReward()
                r.close()
            })
        })
    }

    private ttSidebarReward() {
        let add_seconds = 60*30 //30分钟
        userDataManager.addUserFreeAdsSeconds(add_seconds)
        this.reloadCountdown()
    }
}


