import type { Ref } from 'vue';

import type {
  StoredCardPlanCardViewModel,
  StoredCardPlanEditorForm,
} from '#/views/member/stored-card/types';

import { message } from 'ant-design-vue';

import { saveMemberStoredCardPlanApi } from '#/api/member/stored-card';

import {
  mapPlanEditorFormToSavePayload,
  mapPlanToEditorForm,
  resetPlanEditorForm,
} from './helpers';

interface CreateDrawerActionsOptions {
  canManage: Ref<boolean>;
  drawerMode: Ref<'create' | 'edit'>;
  form: StoredCardPlanEditorForm;
  isDrawerLoading: Ref<boolean>;
  isDrawerOpen: Ref<boolean>;
  isDrawerSubmitting: Ref<boolean>;
  loadPlanList: () => Promise<void>;
  loadRecordList: () => Promise<void>;
  selectedStoreId: Ref<string>;
}

export function createDrawerActions(options: CreateDrawerActionsOptions) {
  function setDrawerOpen(value: boolean) {
    options.isDrawerOpen.value = value;
  }

  function setFormRechargeAmount(value: null | number) {
    options.form.rechargeAmount = value;
  }

  function setFormGiftAmount(value: null | number) {
    options.form.giftAmount = value;
  }

  function setFormSortOrder(value: null | number) {
    options.form.sortOrder = value;
  }

  function setFormStatus(value: 'disabled' | 'enabled') {
    options.form.status = value;
  }

  function openCreateDrawer() {
    if (!options.canManage.value) {
      return;
    }

    options.drawerMode.value = 'create';
    resetPlanEditorForm(options.form);
    options.isDrawerOpen.value = true;
  }

  function openEditDrawer(row: StoredCardPlanCardViewModel) {
    if (!options.canManage.value) {
      return;
    }

    options.drawerMode.value = 'edit';
    Object.assign(options.form, mapPlanToEditorForm(row));
    options.isDrawerOpen.value = true;
  }

  async function submitDrawer() {
    if (!options.canManage.value) {
      return;
    }

    if (!options.selectedStoreId.value) {
      message.warning('请先选择门店');
      return;
    }

    const rechargeAmount = Number(options.form.rechargeAmount ?? 0);
    const giftAmount = Number(options.form.giftAmount ?? 0);
    const sortOrder = Number(options.form.sortOrder ?? 100);
    if (!Number.isFinite(rechargeAmount) || rechargeAmount <= 0) {
      message.warning('充值金额必须大于 0');
      return;
    }

    if (!Number.isFinite(giftAmount) || giftAmount < 0) {
      message.warning('赠送金额不能小于 0');
      return;
    }

    if (!Number.isFinite(sortOrder) || sortOrder < 0 || sortOrder > 9999) {
      message.warning('排序值需在 0-9999 之间');
      return;
    }

    options.isDrawerSubmitting.value = true;
    try {
      await saveMemberStoredCardPlanApi(
        mapPlanEditorFormToSavePayload(
          options.form,
          options.selectedStoreId.value,
        ),
      );
      message.success('保存成功');
      options.isDrawerOpen.value = false;
      await Promise.all([options.loadPlanList(), options.loadRecordList()]);
    } catch (error) {
      console.error(error);
      message.error('保存失败');
    } finally {
      options.isDrawerSubmitting.value = false;
    }
  }

  return {
    openCreateDrawer,
    openEditDrawer,
    setDrawerOpen,
    setFormGiftAmount,
    setFormRechargeAmount,
    setFormSortOrder,
    setFormStatus,
    submitDrawer,
  };
}
