<template>
  <div :class="prefixCls">
    <Button v-if="isDev" type="primary" block @click="handleCopy">
      <CopyOutlined class="mr-2" />
      <%_ if (enableTranslation) { _%>
      {{ t('layout.setting.copyBtn') }}
      <%_ } else { _%>
      t$t('layout.setting.copyBtn')$t$
      <%_ } _%>
    </Button>

    <Button color="warning" block @click="handleResetSetting" class="my-3">
      <RedoOutlined class="mr-2" />
      <%_ if (enableTranslation) { _%>
      {{ t('common.resetText') }}
      <%_ } else { _%>
      t$t('common.resetText')$t$
      <%_ } _%>
    </Button>

    <Button color="error" block @click="handleClearAndRedo">
      <RedoOutlined class="mr-2" />
      <%_ if (enableTranslation) { _%>
      {{ t('layout.setting.clearBtn') }}
      <%_ } else { _%>
      t$t('layout.setting.clearBtn')$t$
      <%_ } _%>
    </Button>
  </div>
</template>
<script lang="ts" setup>
  import { unref } from 'vue';
  import { Button } from 'ant-design-vue';
  import { CopyOutlined, RedoOutlined } from '@ant-design/icons-vue';

  import { useAppStore } from '@/store/modules/app';
  import { usePermissionStore } from '@/store/modules/permission';
  import { useMultipleTabStore } from '@/store/modules/multipleTab';
  import { useUserStore } from '@/store/modules/user';

  import { useDesign } from '@begcode/components';
  <%_ if (enableTranslation) { _%>
  import { useI18n } from '@/hooks/web/useI18n';
  <%_ } _%>
  import { useMessage } from '@/hooks/web/useMessage';
  import { copyText } from '@begcode/components';

  import { updateColorWeak } from '@/logics/theme/updateColorWeak';
  import { updateGrayMode } from '@/logics/theme/updateGrayMode';
  import defaultSetting from '@/settings/projectSetting';
  import { updateSidebarBgColor } from '@/logics/theme/updateBackground';

  defineOptions({ name: 'SettingFooter' });

  const isDev = import.meta.env.DEV;

  const permissionStore = usePermissionStore();
  const { prefixCls } = useDesign('setting-footer');
  <%_ if (enableTranslation) { _%>
  const { t } = useI18n();
  <%_ } _%>
  const { createSuccessModal, createMessage } = useMessage();
  const tabStore = useMultipleTabStore();
  const userStore = useUserStore();
  const appStore = useAppStore();

  function handleCopy() {
    copyText(JSON.stringify(unref(appStore.getProjectConfig), null, 2), null).then(() => {
      createSuccessModal({
        title: <% if (enableTranslation) { %>t('layout.setting.operatingTitle')<% } else { %>'t$t('layout.setting.operatingTitle')$t$'<% } %>,
        content: <% if (enableTranslation) { %>t('layout.setting.operatingContent')<% } else { %>'t$t('layout.setting.operatingContent')$t$'<% } %>,
      });
    });
  }

  function handleResetSetting() {
    try {
      appStore.setProjectConfig(defaultSetting);
      const { colorWeak, grayMode } = defaultSetting;
      updateSidebarBgColor();
      updateColorWeak(colorWeak);
      updateGrayMode(grayMode);
      createMessage.success(<% if (enableTranslation) { %>t('layout.setting.resetSuccess')<% } else { %>'t$t('layout.setting.resetSuccess')$t$'<% } %>);
    } catch (error: any) {
      createMessage.error(error);
    }
  }

  function handleClearAndRedo() {
    localStorage.clear();
    appStore.resetAllState();
    permissionStore.resetState();
    tabStore.resetState();
    userStore.resetState();
    location.reload();
  }
</script>
<style lang="less" scoped>
  @prefix-cls: ~'@{namespace}-setting-footer';

  .@{prefix-cls} {
    display: flex;
    flex-direction: column;
    align-items: center;
  }
</style>
