import { useState, useEffect, useMemo } from 'react';
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';

// 扩展dayjs支持duration
dayjs.extend(duration);

export interface CountdownTime {
  hours: number;
  minutes: number;
  seconds: number;
  isExpired: boolean;
  totalMinutes: number; // 总剩余分钟数
}

export interface UsePaymentCountdownParams {
  /** 订单创建时间（用于兼容：当 expirationTime 缺失时按 createTime + timeoutHours 计算） */
  createTime?: string;
  /** 订单过期时间（推荐直接传后端返回的 expirationTime） */
  expirationTime?: string;
  /** 支付状态 */
  payStatus?: boolean;
  /** 是否实时更新（详情页用true，列表页用false） */
  realTime?: boolean;
  /** 超时小时数，默认24小时（仅在 expirationTime 缺失时生效） */
  timeoutHours?: number;
}

/**
 * 支付倒计时Hook
 * 优先使用 expirationTime；当 expirationTime 缺失时回退到 createTime + timeoutHours。
 */
export const usePaymentCountdown = ({
  createTime,
  expirationTime,
  payStatus,
  realTime = false,
  timeoutHours = 24
}: UsePaymentCountdownParams): CountdownTime => {
  const [timeLeft, setTimeLeft] = useState<CountdownTime>({
    hours: 0,
    minutes: 0,
    seconds: 0,
    isExpired: false,
    totalMinutes: 0
  });

  // 计算剩余时间的函数
  const calculateTimeLeft = useMemo(() => {
    return (): CountdownTime => {
      if (payStatus || (!expirationTime && !createTime)) {
        return {
          hours: 0,
          minutes: 0,
          seconds: 0,
          isExpired: false,
          totalMinutes: 0
        };
      }

      // 优先使用后端过期时间；如果无法解析，再回退到 createTime + timeoutHours
      const expireTimeFromExpiration = expirationTime ? dayjs(expirationTime) : null;
      const expireTimeFromCreate =
        createTime ? dayjs(createTime).add(timeoutHours, 'hour') : null;
      const expireTime =
        expireTimeFromExpiration?.isValid()
          ? expireTimeFromExpiration
          : expireTimeFromCreate?.isValid()
            ? expireTimeFromCreate
            : null;

      if (!expireTime) {
        return {
          hours: 0,
          minutes: 0,
          seconds: 0,
          isExpired: true,
          totalMinutes: 0
        };
      }

      const now = dayjs();
      const diff = expireTime.diff(now);

      if (diff <= 0) {
        return {
          hours: 0,
          minutes: 0,
          seconds: 0,
          isExpired: true,
          totalMinutes: 0
        };
      }

      const durationObj = dayjs.duration(diff);
      const hours = Math.floor(durationObj.asHours());
      const minutes = durationObj.minutes();
      const seconds = durationObj.seconds();
      const totalMinutes = Math.floor(durationObj.asMinutes());

      return {
        hours,
        minutes,
        seconds,
        isExpired: false,
        totalMinutes
      };
    };
  }, [createTime, expirationTime, payStatus, timeoutHours]);

  useEffect(() => {
    if (payStatus || (!expirationTime && !createTime)) {
      setTimeLeft({
        hours: 0,
        minutes: 0,
        seconds: 0,
        isExpired: false,
        totalMinutes: 0
      });
      return;
    }

    // 立即计算一次
    const initialTime = calculateTimeLeft();
    setTimeLeft(initialTime);

    // 如果不需要实时更新，直接返回
    if (!realTime) {
      return;
    }

    // 如果需要实时更新，设置定时器
    const timer = setInterval(() => {
      const newTimeLeft = calculateTimeLeft();
      setTimeLeft(newTimeLeft);
      
      // 如果已过期，清除定时器
      if (newTimeLeft.isExpired) {
        clearInterval(timer);
      }
    }, 1000);

    return () => clearInterval(timer);
  }, [createTime, expirationTime, payStatus, realTime, calculateTimeLeft]);

  return timeLeft;
};

/**
 * 格式化倒计时文本
 * @param timeLeft 倒计时时间对象
 * @param showSeconds 是否显示秒数
 */
export const formatCountdownText = (
  timeLeft: CountdownTime,
  showSeconds: boolean = false
): string => {
  if (timeLeft.isExpired) {
    return '已过期';
  }

  if (timeLeft.hours > 0) {
    if (showSeconds) {
      return `${timeLeft.hours}小时${timeLeft.minutes}分${timeLeft.seconds}秒`;
    } else {
      return `${timeLeft.hours}小时${timeLeft.minutes}分钟`;
    }
  } else if (timeLeft.minutes > 0) {
    if (showSeconds) {
      return `${timeLeft.minutes}分${timeLeft.seconds}秒`;
    } else {
      return `${timeLeft.minutes}分钟`;
    }
  } else {
    return `${timeLeft.seconds}秒`;
  }
};

/**
 * 判断是否为紧急状态（剩余时间少于1小时）
 */
export const isUrgentCountdown = (timeLeft: CountdownTime): boolean => {
  return !timeLeft.isExpired && timeLeft.totalMinutes < 60;
};

/**
 * 判断是否为非常紧急状态（剩余时间少于10分钟）
 */
export const isCriticalCountdown = (timeLeft: CountdownTime): boolean => {
  return !timeLeft.isExpired && timeLeft.totalMinutes < 10;
};

export default usePaymentCountdown;
