/**
 * 时间工具函数
 * 统一处理时区转换，确保显示东八区（UTC+8）时间
 */

/**
 * 将UTC时间转换为东八区时间显示（仅时间）
 * @param {string|Date} timestamp - UTC时间戳
 * @returns {string} 格式化的时间字符串，如 "15:30"
 */
export const formatTimeToUTC8 = (timestamp) => {
  if (!timestamp) return '';
  const date = new Date(timestamp);
  return date.toLocaleTimeString('zh-CN', { 
    hour: '2-digit', 
    minute: '2-digit',
    timeZone: 'Asia/Shanghai'
  });
};

/**
 * 将UTC时间转换为东八区日期时间显示（完整）
 * @param {string|Date} timestamp - UTC时间戳
 * @returns {string} 格式化的日期时间字符串，如 "2024/04/18 15:30:45"
 */
export const formatDateTimeToUTC8 = (timestamp) => {
  if (!timestamp) return '';
  const date = new Date(timestamp);
  return date.toLocaleString('zh-CN', { 
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit', 
    minute: '2-digit',
    second: '2-digit',
    timeZone: 'Asia/Shanghai'
  });
};

/**
 * 将UTC时间转换为东八区日期显示（仅日期）
 * @param {string|Date} timestamp - UTC时间戳
 * @returns {string} 格式化的日期字符串，如 "2024/04/18"
 */
export const formatDateToUTC8 = (timestamp) => {
  if (!timestamp) return '';
  const date = new Date(timestamp);
  return date.toLocaleDateString('zh-CN', { 
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    timeZone: 'Asia/Shanghai'
  });
};

/**
 * 智能时间显示：根据时间远近选择显示方式
 * - 今天：只显示时间 "15:30"
 * - 昨天：显示 "昨天 15:30"
 * - 一周内：显示 "周三 15:30"
 * - 更早：显示 "04/18 15:30"
 * @param {string|Date} timestamp - UTC时间戳
 * @returns {string} 智能格式化的时间字符串
 */
export const formatSmartTime = (timestamp) => {
  if (!timestamp) return '';
  
  const date = new Date(timestamp);
  const now = new Date();
  
  // 使用东八区时间进行日期比较
  // 将UTC时间转换为东八区的日期字符串,然后创建Date对象进行比较
  const dateStr = date.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
  const nowStr = now.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
  
  const dateUTC8 = new Date(dateStr);
  const nowUTC8 = new Date(nowStr);
  
  // 获取今天的开始时间（东八区）
  const today = new Date(nowUTC8.getFullYear(), nowUTC8.getMonth(), nowUTC8.getDate());
  const messageDate = new Date(dateUTC8.getFullYear(), dateUTC8.getMonth(), dateUTC8.getDate());
  
  const diffDays = Math.floor((today - messageDate) / (1000 * 60 * 60 * 24));
  
  if (diffDays === 0) {
    // 今天：只显示时间
    return formatTimeToUTC8(timestamp);
  } else if (diffDays === 1) {
    // 昨天
    return `昨天 ${formatTimeToUTC8(timestamp)}`;
  } else if (diffDays < 7) {
    // 一周内：显示星期
    const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    // 使用东八区时间获取星期
    const dayOfWeek = dateUTC8.getDay();
    return `${weekdays[dayOfWeek]} ${formatTimeToUTC8(timestamp)}`;
  } else {
    // 更早：显示月/日 时间
    return date.toLocaleString('zh-CN', {
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      timeZone: 'Asia/Shanghai'
    });
  }
};

/**
 * 相对时间显示：显示"刚刚"、"5分钟前"等
 * @param {string|Date} timestamp - UTC时间戳
 * @returns {string} 相对时间字符串
 */
export const formatRelativeTime = (timestamp) => {
  if (!timestamp) return '';
  
  const now = new Date();
  const date = new Date(timestamp);
  const diff = now - date;
  
  const seconds = Math.floor(diff / 1000);
  const minutes = Math.floor(seconds / 60);
  const hours = Math.floor(minutes / 60);
  const days = Math.floor(hours / 24);
  
  if (seconds < 60) return '刚刚';
  if (minutes < 60) return `${minutes}分钟前`;
  if (hours < 24) return `${hours}小时前`;
  if (days < 7) return `${days}天前`;
  
  // 超过7天显示具体日期
  return formatDateToUTC8(timestamp);
};

/**
 * 格式化时间戳为易读格式（带秒）
 * @param {string|Date} timestamp - UTC时间戳
 * @returns {string} 格式化的时间字符串，如 "15:30:45"
 */
export const formatTimeWithSeconds = (timestamp) => {
  if (!timestamp) return '';
  const date = new Date(timestamp);
  return date.toLocaleTimeString('zh-CN', { 
    hour: '2-digit', 
    minute: '2-digit',
    second: '2-digit',
    timeZone: 'Asia/Shanghai'
  });
};

/**
 * 获取当前东八区时间
 * @returns {Date} 当前东八区时间的Date对象
 */
export const getCurrentUTC8Time = () => {
  // 获取当前时间的东八区表示
  const now = new Date();
  const utc8String = now.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
  return new Date(utc8String);
};

/**
 * 判断是否是今天（东八区）
 * @param {string|Date} timestamp - UTC时间戳
 * @returns {boolean} 是否是今天
 */
export const isToday = (timestamp) => {
  if (!timestamp) return false;
  
  const date = new Date(timestamp);
  const now = new Date();
  
  // 使用东八区时间进行比较
  const dateStr = date.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
  const nowStr = now.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
  
  const dateUTC8 = new Date(dateStr);
  const nowUTC8 = new Date(nowStr);
  
  return dateUTC8.getFullYear() === nowUTC8.getFullYear() &&
         dateUTC8.getMonth() === nowUTC8.getMonth() &&
         dateUTC8.getDate() === nowUTC8.getDate();
};

/**
 * 判断是否是昨天（东八区）
 * @param {string|Date} timestamp - UTC时间戳
 * @returns {boolean} 是否是昨天
 */
export const isYesterday = (timestamp) => {
  if (!timestamp) return false;
  
  const date = new Date(timestamp);
  const now = new Date();
  
  // 使用东八区时间进行比较
  const dateStr = date.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
  const nowStr = now.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
  
  const dateUTC8 = new Date(dateStr);
  const nowUTC8 = new Date(nowStr);
  
  const yesterday = new Date(nowUTC8);
  yesterday.setDate(yesterday.getDate() - 1);
  
  return dateUTC8.getFullYear() === yesterday.getFullYear() &&
         dateUTC8.getMonth() === yesterday.getMonth() &&
         dateUTC8.getDate() === yesterday.getDate();
};
