import { _decorator, EventTouch, Label, Slider, Sprite, SpriteFrame } from 'cc';
import { settingData } from '../data/SettingData';
import { BaseExp } from '../core/base/BaseExp';
import { autoBind } from '../extend/AutoBind';
const { ccclass, property } = _decorator;

//设置页面
@ccclass("SettingUI")
export class SettingUI extends BaseExp {
    @property({type:SpriteFrame,tooltip:"震动开图片"})
    public vibrate_open: SpriteFrame = null!;
    @property({type:SpriteFrame,tooltip:"震动关闭图片"})
    public vibrate_close: SpriteFrame = null!;
    @property({type:SpriteFrame,tooltip:"背景音乐开图片"})
    public bg_music_open: SpriteFrame = null!;
    @property({type:SpriteFrame,tooltip:"背景音乐关图片"})
    public bg_music_close: SpriteFrame = null!;
    @property({type:SpriteFrame,tooltip:"音效开图片"})
    public sound_open: SpriteFrame = null!;
    @property({type:SpriteFrame,tooltip:"音效关图片"})
    public  sound_close: SpriteFrame = null!;

    @autoBind({type:Sprite,tooltip:"背景音乐节点"})
    public vibrate_sp: Sprite = null!;
    @autoBind({type:Sprite,tooltip:"背景音乐勾节点"})
    public bgMusic_sp: Sprite = null!;
    @autoBind({type:Sprite,tooltip:"音效节点"})
    public sound_sp: Sprite = null!;
    @autoBind({type:Slider,tooltip:"滑块节点"})
    public slider: Slider = null!;
    @autoBind({type:Label,tooltip:"滑块数值节点文本"})
    public speed_num_label: Label = null!;

    public show () {
        this.hasAnim = true;
        //加载数据ui
        this.loadDataUI();
    }

    /**
     * 加载刷新数据ui
     */
    public loadDataUI(){
        this.bgMusic_sp.spriteFrame = settingData.data.bgMusic ? this.bg_music_open : this.bg_music_close;
        this.sound_sp.spriteFrame = settingData.data.soundFx ? this.sound_open : this.sound_close;
        this.vibrate_sp.spriteFrame = settingData.data.vibrate ? this.vibrate_open : this.vibrate_close;
        //设置滑块
        this.setSlider();
    }

    /**
     * 设置滑块的值
     */
    public setSlider(){
        this.slider.progress = settingData.data.sensitivity;
        this.speed_num_label.string = Math.round(settingData.data.sensitivity * 100) + '%';
    }

    /**
     * 滑块事件
     * @param event 事件
     * @param customEventData 自定义数据    
     */
    public onSliderChanged(event: EventTouch, customEventData: string) {
        let value = this.slider.progress;
        settingData.data.sensitivity = parseFloat(value.toFixed(1));
        this.setSlider();
    }

    /**
     * 按钮点击事件
     * @param event 事件
     * @param param 参数
     */
    override onBtnClicked(event:EventTouch, param:any) {
        super.onBtnClicked(event,param);
        let btnName = event.target.name;
        if(btnName === 'vibrate_sp'){//震动开关
            settingData.data.vibrate = !settingData.data.vibrate;
        }else if(btnName === 'bgMusic_sp'){//背景开关
            settingData.data.bgMusic = !settingData.data.bgMusic;
        }else if(btnName === 'sound_sp'){//音效开关
            settingData.data.soundFx = !settingData.data.soundFx;
        }
        this.loadDataUI();
    }
}