import {
  _decorator,
  Component,
  Node,
  tween,
  UIOpacity,
  Vec3,
  director,
  Button,
  AudioClip,
  AudioSource,
  Toggle,
} from "cc";
import { AudioManager, BGMType, SFXType } from "./AudioManager";
const { ccclass, property } = _decorator;

@ccclass("StartScene")
export class StartScene extends Component {
  @property(Node)
  background: Node = null;

  @property(Node)
  logo: Node = null;

  @property(Node)
  startButton: Node = null;

  @property(Node)
  setting_node: Node = null;

  @property(Node)
  setting_tab_node: Node = null;

  @property(AudioClip)
  bg_audio: AudioClip = null;

  @property(Toggle)
  bgmToggle: Toggle = null!;

  @property(Toggle)
  sfxToggle: Toggle = null!;

  onLoad() {
    const am = AudioManager.instance;

    // 注册首页 BGM 和音效
    am.registerBGM(BGMType.Home, this.bg_audio);

    // 初始化 Toggle 状态
    this.bgmToggle.isChecked = am.bgmEnabled;
    this.sfxToggle.isChecked = am.sfxEnabled;

    //  am.registerSFX(SFXType.Item, this.startClickClip);
    am.setCurBgm(BGMType.Home);
    am.playBGM(BGMType.Home);
    // 监听切换事件
    this.bgmToggle.node.on("toggle", this.onBgmToggleChanged, this);
    this.sfxToggle.node.on("toggle", this.onSfxToggleChanged, this);
  }

  start() {
    // 初始化透明度
    const bgOpacity = this.background.addComponent(UIOpacity);
    bgOpacity.opacity = 0;

    const logoOpacity = this.logo.addComponent(UIOpacity);
    logoOpacity.opacity = 0;
    this.logo.setScale(new Vec3(0.5, 0.5, 1));

    const btnOpacity = this.startButton.addComponent(UIOpacity);
    btnOpacity.opacity = 0;

    // 背景渐入
    tween(bgOpacity).to(1, { opacity: 255 }).start();

    // Logo 缩放 + 渐入
    tween(logoOpacity).delay(0.5).to(1, { opacity: 255 }).start();

    tween(this.logo)
      .delay(0.5)
      .to(1, { scale: new Vec3(1.1, 1.1, 1) })
      .to(0.3, { scale: new Vec3(1, 1, 1) })
      .start();

    // 按钮呼吸动画
    tween(btnOpacity)
      .delay(1.2)
      .to(0.8, { opacity: 255 })
      .call(() => {
        this.playButtonBreathing();
      })
      .start();

    // 按钮点击事件
    // this.startButton.on(Button.EventType.CLICK, this.onStartGame, this);
  }

  onBgmToggleChanged(toggle: Toggle) {
    AudioManager.instance.setBGMEnabled(toggle.isChecked);
  }

  onSfxToggleChanged(toggle: Toggle) {
    AudioManager.instance.setSFXEnabled(toggle.isChecked);
  }

  playButtonBreathing() {
    tween(this.startButton)
      .repeatForever(
        tween()
          .to(0.6, { scale: new Vec3(1.1, 1.1, 1) })
          .to(0.6, { scale: new Vec3(1, 1, 1) })
      )
      .start();
  }

  onStartGame() {
    // 按钮点击反馈动画
    tween(this.startButton)
      .to(0.1, { scale: new Vec3(0.9, 0.9, 1) })
      .to(0.1, { scale: new Vec3(1, 1, 1) })
      .call(() => {
        director.loadScene("s1"); // 这里替换为你的第一关场景名
      })
      .start();
  }

  onShowSettingTab() {
    this.setting_tab_node.active = true;
  }

  onHideSettingTab() {
    this.setting_tab_node.active = false;
  }

  onSwitchMusic(toggle) {
    // this.audioMgr.bgmSource = this.node.getComponent(AudioSource);

    if (toggle.isChecked) {
      // this.audioMgr.setBGMEnabled(true);
      // this.audioMgr.playBGM(this.bg_audio);
      // this.playBgAudio();
    } else {
      // this.audioMgr.setBGMEnabled(false);
      // this.notPlayBgAudio();
      // this.audioMgr.playBGM(this.bg_audio);
    }
  }

  playBgAudio() {
    const Audio = this.node.getComponent(AudioSource);
    Audio.clip = this.bg_audio;
    Audio.play();
  }

  notPlayBgAudio() {
    const Audio = this.node.getComponent(AudioSource);
    Audio.clip = this.bg_audio;
    Audio.stop();
  }
}
