import { _decorator, Color, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { base_ui } from '../../fw/base_ui';
import { http } from '../../http';
import { config } from '../../config';
import { sign_reward } from './sign_reward';
import { sign_total_reward } from './sign_total_reward';
import { sign_reward_data, sign_total_reward_data } from '../../data';
import { sign_receive_reward } from './sign_receive_reward';
import { GameManager } from '../../GameManager';
import { ClientEvent } from '../../lib/clientEvent';
import { tools } from '../../tools';
const { ccclass, property } = _decorator;

@ccclass('sign_view')
export class sign_view extends base_ui {
    @property(Node) public_bg:Node = null
    @property(Node) reward_node:Node = null
    @property(Node) total_reward_node:Node = null
    @property(Node) btn_sign:Node = null
    @property(Node) lab_sign:Node = null
    @property(SpriteFrame) sf_btn_sign_qd:SpriteFrame = null   // 签到
    @property(SpriteFrame) sf_btn_sign_yqd:SpriteFrame = null  // 已签到

    @property(Node) receive_reward:Node = null

    private m_today_data:sign_reward_data = null
    start() {
        this.onButtonListen(this.public_bg, ()=>{
            this.close()
        })
        this.onButtonListen(this.btn_sign, ()=>{
            this.requestUserSign()
        })

        this.btn_sign.active = false
        GameManager.requestDelay(()=>{
            this.requestSignListData()
        })
    }

    private requestSignListData(cb=null) {
        http.post(config.API.sign_list,null,(err,d)=>{
            if(!err){
                let nd = JSON.parse(d)
                if(nd.code === config.status.SUCCESS){
                    // console.log("sign_list", nd.content)
                    let total_sign_number = nd.content.total_sign_number
                    this.reward_node.getComponent(sign_reward).initView(nd.content.sign_reward_list,this.onRewardCallBack.bind(this),this.onClickRewardItem.bind(this))
                    this.total_reward_node.getComponent(sign_total_reward).initView(total_sign_number, nd.content.sign_total_reward_list,this.onClickTotalRewardItem.bind(this))
                    cb && cb(nd.content)
                }
            }
        },'GET')
    }

    private onRewardCallBack(today_data:sign_reward_data) {
        this.m_today_data = today_data
        if(today_data.receive_status==0) {
            this.btn_sign.getComponent(Sprite).spriteFrame = this.sf_btn_sign_qd
            this.lab_sign.getComponent(Label).color = new Color().fromHEX("#A3511B")
            this.lab_sign.getComponent(Label).string = '签    到'
        } else {
            this.btn_sign.getComponent(Sprite).spriteFrame = this.sf_btn_sign_yqd
            this.lab_sign.getComponent(Label).color = new Color().fromHEX("#464646")
            this.lab_sign.getComponent(Label).string = '今日已签到'
        }
        this.btn_sign.active = true
    }

    private onClickRewardItem(data:sign_reward_data,is_buqian:boolean) {
        let call_back = (()=>{
            this.showReceiveReward(data)
        })
        if(is_buqian) {
            GameManager.showVideoAd(config.ADS_TYPE.GAME_SIGN_BUQIAN, (res)=>{
                call_back()
            })
        } else {
            call_back()
        }
    }

    private onClickTotalRewardItem(data:sign_total_reward_data) {
        this.receive_reward.active = true
        this.receive_reward.getComponent(sign_receive_reward).initReceiveSuccess(data)
        this.requestSignListData((data)=>{
            this.checkSignAll(data)
        })
    }

    private requestUserSign() {
        if(this.m_today_data.receive_status==1) {
            return
        }
        this.showReceiveReward(this.m_today_data)
    }

    private showReceiveReward(data:sign_reward_data) {
        this.receive_reward.active = true
        this.receive_reward.getComponent(sign_receive_reward).initView(data, (v:sign_receive_reward)=>{
            this.requestSignListData((data)=>{
                this.checkSignAll(data)
            })
        })
    }

    private checkSignAll(data) {
        let reward_list:sign_reward_data[] = data.sign_reward_list
        let is_sign_all = true
        for (let index = 0; index < reward_list.length; index++) {
            const element = reward_list[index];
            if(element.current_sign_status==0&&element.repair_sign_status==1)  {
                is_sign_all = false
                break
            }
            if(element.current_sign_status==1&&element.receive_status==0) {
                is_sign_all = false
                break
            }
        }

        if(is_sign_all) {
            let total_reward_list:sign_total_reward_data[] = data.sign_total_reward_list
            for (let index = 0; index < total_reward_list.length; index++) {
                const element = total_reward_list[index];
                if(element.is_receive_status==1&&element.receive_status==0) {
                    is_sign_all = false
                    break
                }
            }
        }
        
        if(is_sign_all) {
            tools.user_red_dot_data.sign_red_dot = 0
            ClientEvent.dispatchEvent(config.UI_EVENT.UPDATE_SIGN_RED_DOT_STATUS)
        }
    }

}

