import { config } from "../../constants/config";

const { regClass, property } = Laya;

@regClass()
export class CalendarUnit extends Laya.Script {
    declare owner : Laya.Box;

    @property(Number)
    public year: number = 0;
    @property(Number)
    public month: number = 0;
    @property(Number)
    public day: number = 0;
    @property(Boolean)
    public open: boolean = false;
    @property(Number)
    public progress: number = 0;

    private selected: boolean = false

    @property(Laya.Sprite)
    public obj_selected: Laya.Sprite;

    @property(Laya.Box)
    public obj_pie: Laya.Box;
    @property(Laya.Sprite)
    public sprite_pie: Laya.Sprite;

    @property(Laya.Sprite)
    public obj_mask: Laya.Sprite;
    @property(Laya.Sprite)
    public sprite_mask: Laya.Sprite;

    @property(Laya.Image)
    public obj_star: Laya.Image;

    @property(Laya.Label)
    public label_show: Laya.Label;


    //组件被激活后执行，此时所有节点和组件均已创建完毕，此方法只执行一次
    onAwake(): void {

    }

    public onInit(label_obj: Laya.Label): void {
        this.label_show = label_obj
    }

    public setCalendarUnit(year: number, month: number, day: number, open: boolean, progress: number, handler: any, func: any): void {
        this.selected = false
        this.year = year
        this.month = month
        this.day = day
        this.open = open
        this.progress = progress
        if (open) {
            if (progress >= 1) {
                this.label_show.text = ""
            }
            else {
                this.label_show.text = this.day.toString()
            }
            this.label_show.color = "#2d3138"
            this.owner.on(Laya.Event.CLICK, handler, func)
            this.updateUnit()
        }
        else {
            this.label_show.text = this.day.toString()
            this.label_show.color = "#d3d5db"
            this.owner.on(Laya.Event.CLICK, this, (evt: Laya.Event)=>{

            })
            this.obj_selected.visible = false
            this.obj_pie.visible = false
            this.obj_mask.visible = false
            this.obj_star.visible = false
        }
    }

    public setSelected(selected: boolean): void {
        this.selected = selected
        this.updateUnit()
    }

    updateUnit(): void {
        if (this.open == false) {
            return
        }

        this.obj_star.visible = this.progress >= 1

        var blockSize = 48
        if (config.H_SCREEN)
            blockSize = 33
        
        if (this.selected) {
            this.label_show.color = "#ffffff"
            this.obj_selected.visible = true
            if (this.progress > 0) {
                this.obj_pie.visible = true
                this.obj_mask.visible = true
                this.sprite_pie.graphics.clear()
                this.sprite_pie.graphics.drawPie(blockSize, blockSize, blockSize, 0, 360*this.progress, "#ffffff")
                this.sprite_mask.graphics.clear()
                this.sprite_mask.graphics.drawCircle(0.5, 0.5, 0.5, "#1d5cdc")
            }
            else {
                this.obj_pie.visible = false
                this.obj_mask.visible = false 
            }
        }
        else {
            this.label_show.color = "#2d3138"
            this.obj_selected.visible = false
            if (this.progress > 0 && this.progress < 1) {
                this.obj_pie.visible = true
                this.obj_mask.visible = true
                this.sprite_pie.graphics.clear()
                this.sprite_pie.graphics.drawPie(blockSize, blockSize, blockSize, 0, 360*this.progress, "#2d3138")
                this.sprite_mask.graphics.clear()
                this.sprite_mask.graphics.drawCircle(0.5, 0.5, 0.5, "#ffffff")
            }
            else {
                this.obj_pie.visible = false
                this.obj_mask.visible = false
            }            
        }
    }
}