import { defineStore } from 'pinia';
import { useTourStore } from './tour';
import { getApp, useApp } from '/@/hooks/userApp';

interface AppState {
  panoId: Nullable<string>;
  isLoaded: boolean;
  mode: string;
  flying: boolean;
  floorId: Nullable<string>;
  player: {
    lang: string;
    showVR: boolean;
    showMore: boolean;
    showDescription: boolean;
    showToolbar: boolean;
    showMap: boolean;
    showShare: boolean;
    showUserGuide: boolean;
    showWidgets: boolean;
  };
  router: any;
  controlsBottom: string;
  isTourMode: boolean; // 自由观看模式
  ttl: number;
}

export const useAppStore = defineStore({
  id: 'app',
  state: (): AppState => ({
    isLoaded: false,
    panoId: null,
    mode: 'panorama',
    flying: false,
    floorId: null,
    player: {
      lang: 'zh',
      showVR: false,
      showMore: false,
      showDescription: false,
      showToolbar: true,
      showMap: true,
      showShare: false,
      showUserGuide: false,
      showWidgets: true,
    },
    router: {},
    controlsBottom: '20px',
    isTourMode: true,
    ttl: -1,
  }),
  getters: {},
  actions: {
    setRoomValidTime(ttl: number): void {
      this.ttl = ttl;
    },
    setIsTourMode(payload: boolean): void {
      this.isTourMode = payload;
      const searchParams = new URLSearchParams(location.search);
      if (!searchParams.has('isTour')) {
        const isTour = payload ? '1' : '0';
        searchParams.set('isTour', isTour);
        location.search = searchParams.toString();
      }
    },
    setMode(payload: string): void {
      console.log('setMode', payload);
      if (payload == this.mode) {
        return;
      }
      this.flying = true;
      useApp().then((sdk) => {
        sdk.Camera[payload]()
          .then(() => {
            this.flying = false;
          })
          .catch(() => {
            this.flying = false;
          });
      });

      // if (payload == 'dollhouse') {
      //   // setTimeout(() => {
      //   //   this.setFloor('all');
      //   // }, 500);
      // }
      this.mode = payload;
    },
    setFloorId(id: string): void {
      console.log('setFloor-2', id);
      this.floorId = id;
    },
    setFlying(payload: boolean): void {
      this.flying = payload;
    },
    setFloor(id: string): void {
      this.floorId = id;
      console.log('setFloor-1', id);
      getApp().Scene.gotoFloor(id);
    },
    showVR(): void {
      this.player.showVR = !this.player.showVR;
      this.player.showWidgets = !this.player.showWidgets;
      getApp().Camera.vr();
    },
    showUserGuide(payload: boolean): void {
      this.player.showUserGuide = payload;
    },
    setControlsBottom(payload: string): void {
      this.controlsBottom = payload;
    },
    setPanoId(id: string) {
      this.panoId = id;
    },
    setPlayerOptions(payload) {
      for (const key in payload) {
        this.player[key] = payload[key];
        if (key == 'showSplit') {
          this.player.showWidgets = !payload[key];
        }
      }
      if (!this.player.showToolbar || this.player.showVR) {
        const tourStore = useTourStore();
        console.log('tourStore', tourStore);
        // debugger
        // this.commit('tour/setData', { showTours: false })
        // tourStore.setData({ showTours: false })
      }
    },
    isLoad() {
      this.isLoaded = true;
    },
    unLoad() {
      this.isLoaded = false;
    },
  },
});
