import Taro from '@tarojs/taro';

import { List, PullRefresh } from '@taroify/core';
import classNames from 'classnames';
import { isEqual } from 'lodash-es';
import { useCallback, useEffect, useRef, useState } from 'react';

import JobManageCard from '@/components/job-manage-card';
import ListPlaceholder from '@/components/list-placeholder';
import { EventName } from '@/constants/app';
import { GetJobManagesRequest, JobManageInfo } from '@/types/job';
import { logWithPrefix } from '@/utils/common';
import { requestJobManageList as requestData } from '@/utils/job';

import './index.less';

interface IRequestProps extends Partial<GetJobManagesRequest> {}

export interface IJobManageListProps extends IRequestProps {
  visible?: boolean;
  refreshDisabled?: boolean;
  listHeight?: number;
  className?: string;
  onListEmpty?: () => void;
}

const FIRST_PAGE = 0;
const PAGE_SIZE = 40;
const PREFIX = 'job-manage-list';
const log = logWithPrefix(PREFIX);

function JobManageList(props: IJobManageListProps) {
  const { className, listHeight, refreshDisabled, visible = true, status, onListEmpty } = props;
  const [refreshing, setRefreshing] = useState(false);
  const [hasMore, setHasMore] = useState(true);
  const [loadingMore, setLoadingMore] = useState(false);
  const [loadMoreError, setLoadMoreError] = useState(false);
  const [dataList, setDataList] = useState<JobManageInfo[]>([]);
  const currentPage = useRef<number>(FIRST_PAGE);
  const requestProps = useRef<IRequestProps>({});
  const prevRequestProps = useRef<IRequestProps>({});
  const onListEmptyRef = useRef(onListEmpty);

  const handleRefresh = useCallback(async () => {
    log('start pull refresh');
    try {
      setRefreshing(true);
      setLoadMoreError(false);
      const { page, hasMore: more, jobResults } = await requestData({ ...requestProps.current, page: 1 });
      setHasMore(more);
      setDataList(jobResults);
      currentPage.current = page;
      !jobResults.length && onListEmptyRef.current?.();
      log('pull refresh success');
    } catch (e) {
      setDataList([]);
      setHasMore(false);
      setLoadMoreError(true);
      currentPage.current = FIRST_PAGE;
      log('pull refresh failed');
    } finally {
      setRefreshing(false);
    }
  }, []);

  const handleLoadMore = useCallback(async () => {
    log('start load more', hasMore);
    if (!hasMore) {
      return;
    }
    setLoadMoreError(false);
    setLoadingMore(true);
    try {
      const {
        page,
        hasMore: more,
        jobResults,
      } = await requestData({ ...requestProps.current, page: currentPage.current + 1 });
      setDataList([...dataList, ...jobResults]);
      setHasMore(more);
      currentPage.current = page;
      log('load more success');
    } catch (e) {
      setLoadMoreError(true);
      log('load more failed');
    } finally {
      setLoadingMore(false);
    }
  }, [dataList, currentPage, hasMore]);

  useEffect(() => {
    onListEmptyRef.current = onListEmpty;
  }, [onListEmpty]);

  useEffect(() => {
    log('request params changed');
    requestProps.current = { status: status, pageSize: PAGE_SIZE };
  }, [status]);

  // 初始化数据&配置变更后刷新数据
  useEffect(() => {
    // 相比前一次可见时没有数据变更时，不再重新请求
    if (isEqual(prevRequestProps.current, requestProps.current)) {
      log('visible/city changed, but request params not change, ignore');
      return;
    }
    // 列表不可见时，先不做处理
    if (!visible) {
      log('visible/city changed, but is not visible, only clear list');
      return;
    }

    prevRequestProps.current = requestProps.current;
    const refresh = async () => {
      log('visible/city changed, start refresh list data');
      try {
        setDataList([]);
        setLoadingMore(true);
        setLoadMoreError(false);
        const { page, hasMore: more, jobResults } = await requestData({ ...requestProps.current, page: 1 });
        setHasMore(more);
        setDataList(jobResults);
        currentPage.current = page;
        !jobResults.length && onListEmptyRef.current?.();
      } catch (e) {
        setDataList([]);
        setHasMore(false);
        setLoadMoreError(true);
        currentPage.current = FIRST_PAGE;
      } finally {
        log('visible/city changed, refresh list data end');
        setLoadingMore(false);
      }
    };
    refresh();
  }, [visible, status]);

  useEffect(() => {
    Taro.eventCenter.on(EventName.COMPANY_JOB_PUBLISH_CHANGED, handleRefresh);
    return () => {
      Taro.eventCenter.off(EventName.COMPANY_JOB_PUBLISH_CHANGED, handleRefresh);
    };
  }, [handleRefresh]);

  // log('render', `hasMore: ${hasMore}, loadingMore: ${loadingMore}, refreshing: ${refreshing}`);

  return (
    <PullRefresh
      className={classNames(`${PREFIX}__pull-refresh`, className)}
      loading={refreshing}
      onRefresh={handleRefresh}
      disabled={refreshDisabled}
    >
      <List
        hasMore={hasMore}
        onLoad={handleLoadMore}
        loading={loadingMore || refreshing}
        disabled={loadMoreError}
        fixedHeight={typeof listHeight !== 'undefined'}
        style={listHeight ? { height: `${listHeight}px` } : undefined}
      >
        {dataList.map(item => (
          <JobManageCard data={item} key={item.id} />
        ))}
        <ListPlaceholder hasMore={hasMore} loadingMore={loadingMore} loadMoreError={loadMoreError} noMoreText="" />
      </List>
    </PullRefresh>
  );
}

export default JobManageList;
