import { _decorator, Component, Label, log, Node, Sprite, SpriteFrame, sys } from 'cc';
import { SdkUtil } from '../framework/sdkUtil';
import { gameManager } from '../gameManager';
import { config } from '../config';
const { ccclass, property } = _decorator;

@ccclass('reward_box_page')
export class reward_box_page extends Component {
    @property(Node) btn_close:Node = null;
    @property(Node) lab_coin:Node = null;
    @property(SpriteFrame) sf_enterSidebar:SpriteFrame;
    @property(SpriteFrame) sf_getReward:SpriteFrame;
    @property(Node) btn_enter:Node = null

    private m_coin_number:number = 0
    private m_isToEnterFromSidebar:boolean = true
    private m_receiveRewardCallback:any = null

    showView(coin_number:number, sign_status:number, isToEnterFromSidebar:boolean, receiveRewardCallback:any) {
        this.m_coin_number = coin_number
        this.m_isToEnterFromSidebar = isToEnterFromSidebar
        this.m_receiveRewardCallback = receiveRewardCallback

        this.lab_coin.getComponent(Label).string = '+' + coin_number + '金币'

        if(this.m_isToEnterFromSidebar) {
            if(sign_status == config.SIGN_STATUS_TYPE.GET) {
                this.btn_enter.getComponent(Sprite).spriteFrame = this.sf_getReward
            } else {
                // this.btn_enter.children[0].getComponent(Label).string = '今日已领取'
            }
        } else {
            this.btn_enter.getComponent(Sprite).spriteFrame = this.sf_enterSidebar
        }

        let self = this
        this.btn_close.on(Node.EventType.TOUCH_END, ()=> {
            self.close()  
        })
        this.btn_enter.on(Node.EventType.TOUCH_END, ()=> {
            if(self.m_isToEnterFromSidebar) {
                if(sign_status == config.SIGN_STATUS_TYPE.GET) {
                    // 领取奖励
                    if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
                        gameManager.request_sign_status(config.SIGN_STATUS_TYPE.SYNC, (data)=> {
                            gameManager.userSignStatus.status = 1
                            if(self.m_receiveRewardCallback!=null) {
                                self.m_receiveRewardCallback(self.m_coin_number, self)
                            }
                        }, (err)=> {})
                    }
                } else {
                    // 已领取
                    self.close()
                }
            } else {
                SdkUtil.ttNavToSidebarScene()
                self.close()
            }
        })
    }

    close() {
        this.node.removeFromParent()
    }
}

