import { Model, routerRedux } from 'dva';
import { message, Modal } from 'antd';
import { Journal, FundBasic } from '@/types/common';
import { getFundList } from './service';
import { checkFundPwd, getFundBasic } from '@/services/common';
import { formatMessage } from 'umi-plugin-locale';
import { dispatch } from '@/utils/utils';

export interface HomeFundModelState {
  refreshingList: boolean;
  loadingList: boolean;
  loadingFinished: boolean;
  errorList: boolean;
  data: Journal[] | null;

  refreshingBasic: boolean;
  errorBasic: boolean;
  info: FundBasic | null;

  pageIndex: number;
  loadingDraw: boolean;
}

const FundModel: Model = {
  namespace: 'homeFund',

  state: {
    refreshingList: false,
    loadingList: false,
    loadingFinished: false,
    errorList: false,
    data: null,

    refreshingBasic: false,
    errorBasic: false,
    info: null,

    pageIndex: 1,
    loadingDraw: false,
  } as HomeFundModelState,

  effects: {
    *loadList({ payload: refresh }, { put, call, select }) {
      const {
        refreshingList,
        loadingList,
        pageIndex,
        data,
      } = yield select((state: any) => state.homeFund);
      if (refreshingList || loadingList) return;

      yield put({
        type: 'onUpdateState',
        refreshingList: refresh,
        loadingList: !refresh,
        errorList: false,
      });
      try {
        const pageSize = 10;
        const page = refresh ? 1 : pageIndex + 1;
        const list = yield call(getFundList, page, pageSize);
        yield put({
          type: 'onUpdateState',
          data: refresh ? list : [...data, ...list],
          refreshingList: false,
          loadingList: false,
          pageIndex: page,
          errorList: false,
          loadingFinished: list.length !== pageSize,
        });
      } catch (e) {
        message.error(e.message);
        yield put({
          type: 'onUpdateState',
          refreshingList: false,
          loadingList: false,
          errorList: true,
        });
      }
    },

    *loadBasic(_, { put, call, select }) {
      const { refreshingBasic } = yield select((state: any) => state.homeFund);
      if (refreshingBasic) return;

      yield put({
        type: 'onUpdateState',
        refreshingBasic: true,
        errorBasic: false,
      });
      try {
        const info = yield call(getFundBasic);
        yield put({
          type: 'onUpdateState',
          info,
          refreshingBasic: false,
          errorBasic: false,
        });
      } catch (e) {
        yield put({
          type: 'onUpdateState',
          refreshingBasic: false,
          errorBasic: true,
        });
      }
    },

    *gotoDraw(_, { put, select, call }) {
      const { loadingDraw } = yield select((state: any) => state.homeFund);
      if (loadingDraw) return;
      yield put({ type: 'onUpdateState', loadingDraw: true });
      try {
        const settled = yield call(checkFundPwd);
        if (settled) {
          yield put(routerRedux.push('/draw'));
        } else {
          Modal.confirm({
            okText: formatMessage({ id: 'common.ok' }),
            cancelText: formatMessage({ id: 'common.cancel' }),
            title: formatMessage({ id: 'common.prompt' }),
            content: formatMessage({ id: 'home.fund.prompt' }),
            onOk: () => dispatch(routerRedux.push('/fundpwd')),
          });
        }
      } catch (e) {
        message.error(e.message);
      } finally {
        yield put({ type: 'onUpdateState', loadingDraw: false });
      }
    },
  },

  reducers: {
    onUpdateState(state, payload) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default FundModel;
