import { defineComponent, ref, computed, PropType, toRef, onMounted } from 'vue'
import { useUserStore } from '@store/UserStore'
import { useRouter } from 'vue-router'
import { useGlobalStore } from '@store/globalStore'
import { useI18n } from 'vue-i18n'
import { tools } from '@tools'
import { costanti, IMainCard } from '@store/Modules/costanti'
import { CBigBtn } from '@src/components/CBigBtn'


export default defineComponent({
  name: 'CCheckAppRunning',
  components: { CBigBtn },
  props: {},
  setup(props, { emit }) {

    const userStore = useUserStore()
    const $router = useRouter()
    const globalStore = useGlobalStore()
    const { t } = useI18n()

    const isAppRunning = computed(() => globalStore.isAppRunning)

    const finishLoading = computed(() => globalStore.finishLoading)

    const deferredPrompt = computed(() => globalStore.deferredPrompt)
    const homescreen = computed(() => globalStore.homescreen)


    const viewiOS = ref(false)
    const viewAndroid = ref(false)

    function installApp() {
      if (globalStore.deferredPrompt) {
        globalStore.deferredPrompt.prompt()

        // Wait for the user to respond to the prompt
        globalStore.deferredPrompt.userChoice.then((choiceResult: any) => {
          if (choiceResult.outcome === 'accepted') {
            globalStore.deferredPrompt = null;
            // console.log('User accepted the A2HS prompt');
          } else {
            // console.log('User dismissed the A2HS prompt');
          }
        });
      }

    }

    function mounted() {
      tools.checkApp()
    }

    onMounted(mounted)

    return {
      userStore,
      tools,
      costanti,
      finishLoading,
      installApp,
      isAppRunning,
      deferredPrompt,
      viewiOS,
      viewAndroid,
      homescreen,
    }
  }
})
