import { _decorator, Component, Label, Node, Sprite, Animation, SpriteFrame } from 'cc';
import { base_ui } from '../../fw/base_ui';
import { sign_reward_data, sign_total_reward_data } from '../../data';
import { tools } from '../../tools';
import { http } from '../../http';
import { config } from '../../config';
import { GameManager } from '../../GameManager';
import { uiManager } from '../../manager/uiManager';
const { ccclass, property } = _decorator;

@ccclass('sign_receive_reward')
export class sign_receive_reward extends base_ui {
    @property(Node) public_bg:Node = null
    @property(Node) img_title:Node = null
    @property(Node) img_icon:Node = null
    @property(Node) lab_name:Node = null
    @property(Node) lab_count:Node = null
    @property(Node) node_btn:Node = null
    @property(Node) btn_receive:Node = null
    @property(Node) btn_double:Node = null

    @property(SpriteFrame) sf_dangrihuode = null
    @property(SpriteFrame) sf_gongxihuode = null
    private m_reward_data:sign_reward_data = null
    private m_cb = null
    start() {
        this.onButtonListen(this.public_bg, ()=>{
            this.closeSelf()
        })
        this.onButtonListen(this.btn_receive, ()=>{
            this.requestUserSign(false)
        })
        this.btn_double.getComponent(Animation).play()
        this.onButtonListen(this.btn_double, ()=>{
            GameManager.showVideoAd(config.ADS_TYPE.GAME_SIGN_DOUBLE, (res)=>{
                this.requestUserSign(true)
            })
        })
    }

    initView(data:sign_reward_data, cb) {
        this.m_reward_data = data
        this.m_cb = cb
        this.img_title.getComponent(Sprite).spriteFrame = this.sf_dangrihuode
        this.node_btn.active = true
        tools.loadRemoteImg(data.icon, (d)=>{
            this.img_icon.getComponent(Sprite).spriteFrame = d.sf
        })
        this.lab_name.getComponent(Label).string = data.name
        this.setLabCount(data.quantity)
    }

    initReceiveSuccess(data:sign_total_reward_data) {
        this.img_title.getComponent(Sprite).spriteFrame = this.sf_gongxihuode
        this.node_btn.active = false
        tools.loadRemoteImg(data.icon, (d)=>{
            this.img_icon.getComponent(Sprite).spriteFrame = d.sf
        })
        this.lab_name.getComponent(Label).string = data.name
        this.setLabCount(data.quantity)
    }

    private setLabCount(count:number) {
        this.lab_count.getComponent(Label).string = `x${count}`
    }

    private showRewardDetails(is_double:boolean) {
        this.img_title.getComponent(Sprite).spriteFrame = this.sf_gongxihuode
        this.node_btn.active = false
        if(is_double) {
            let count = this.m_reward_data.quantity * 2
            this.setLabCount(count)
        }
    }

    private closeSelf() {
        this.btn_double.getComponent(Animation).stop()
        this.node.active = false
    }

    private requestUserSign(is_double:boolean) {
        let stype = 1
        if(is_double) {
            stype = 2
        }
        uiManager.Instance().showLoading()
        let opt = {'index':this.m_reward_data.index, 'stype': stype}
        http.post(config.API.user_sign, opt, (err,d)=>{
            uiManager.Instance().hideLoading()
            if(!err){
                let nd = JSON.parse(d)
                if(nd.code === config.status.SUCCESS){
                    // console.log("user_sign", nd.content)
                    this.showRewardDetails(is_double)
                    this.m_cb && this.m_cb(this)
                }
            }
        })
    }
}

