import { _decorator, Color, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { sign_total_reward_data } from '../../data';
import { base_ui } from '../../fw/base_ui';
import { tools } from '../../tools';
const { ccclass, property } = _decorator;

@ccclass('sign_total_reward_item')
export class sign_total_reward_item extends base_ui {
    @property(Node) img_bg:Node = null
    @property(Node) img_icon:Node = null
    @property(Node) lab_count:Node = null
    @property(Node) lab_name:Node = null
    @property(Node) img_mask:Node = null
    @property(Node) img_dot:Node = null
    @property(Node) lab_days:Node = null
    @property(Node) img_left_line:Node = null
    @property(Node) img_right_line:Node = null

    @property(SpriteFrame) sf_item_default:SpriteFrame = null
    @property(SpriteFrame) sf_item_current:SpriteFrame = null
    @property(SpriteFrame) sf_dot_default:SpriteFrame = null
    @property(SpriteFrame) sf_dot_current:SpriteFrame = null
    @property(SpriteFrame) sf_line_default:SpriteFrame = null
    @property(SpriteFrame) sf_line_current:SpriteFrame = null

    private color_default = new Color().fromHEX("#ffffff")
    private color_current = new Color().fromHEX("#8a3516")
    private m_data:sign_total_reward_data = null
    private m_cb = null
    start() {
        this.onButtonListen(this.img_bg,()=>{
            this.m_cb && this.m_cb(this)
        })
    }

    initView(data:sign_total_reward_data, cb) {
        this.m_cb = cb
        this.setData(data)
    }

    public getData():sign_total_reward_data {
        return this.m_data
    }

    public setData(data:sign_total_reward_data) {
        this.m_data = data
        tools.loadRemoteImg(data.icon, (d)=>{
            this.img_icon.getComponent(Sprite).spriteFrame = d.sf
        })
        this.lab_name.getComponent(Label).string = data.name
        this.lab_count.getComponent(Label).string = `x${data.quantity}`
        this.lab_days.getComponent(Label).string = `${data.day_number}`

        if(data.receive_status==1) {
            // 已经领取
            this.setState(false)
            this.img_mask.active = true
            this.img_bg.getComponent(Sprite).spriteFrame = this.sf_item_default
        } else{
            // 未领取
            this.img_mask.active = false
            if(data.is_receive_status==1) {
                // 可以领取 
                this.setState(false)
            } else {
                // 不能领取 
                this.setState(true)
            }
        }
    }

    setLastReceive() {
        this.img_right_line.getComponent(Sprite).spriteFrame = this.sf_line_default
    }

    private setState(is_default:boolean) {
        if(is_default) {
            this.img_bg.getComponent(Sprite).spriteFrame = this.sf_item_default
            this.img_dot.getComponent(Sprite).spriteFrame = this.sf_dot_default
            this.lab_days.getComponent(Label).color = this.color_default
            this.img_left_line.getComponent(Sprite).spriteFrame = this.sf_line_default
            this.img_right_line.getComponent(Sprite).spriteFrame = this.sf_line_default
        } else {
            this.img_bg.getComponent(Sprite).spriteFrame = this.sf_item_current
            this.img_dot.getComponent(Sprite).spriteFrame = this.sf_dot_current
            this.lab_days.getComponent(Label).color = this.color_current
            this.img_left_line.getComponent(Sprite).spriteFrame = this.sf_line_current
            this.img_right_line.getComponent(Sprite).spriteFrame = this.sf_line_current
        }
    }

    public hideLeftLine() {
        this.img_left_line.active = false
    }

    public hideRightLine() {
        this.img_right_line.active = false
    }

}

