import { useState, useEffect } from 'react';
import Taro from '@tarojs/taro';
import { User } from '@/api/system/user/model';
import { getUserInfo, updateUserInfo } from '@/api/layout';

// 用户Hook
export const useUser = () => {
  const [user, setUser] = useState<User | null>(null);
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  const [loading, setLoading] = useState(true);

  // 从本地存储加载用户数据
  const loadUserFromStorage = () => {
    try {
      const token = Taro.getStorageSync('access_token');
      const userData = Taro.getStorageSync('User');
      const userId = Taro.getStorageSync('UserId');
      const tenantId = Taro.getStorageSync('TenantId');

      if (token && userData) {
        const userInfo = typeof userData === 'string' ? JSON.parse(userData) : userData;
        setUser(userInfo);
        setIsLoggedIn(true);
      } else if (token && userId) {
        // 如果有token和userId但没有完整用户信息，标记为已登录但需要获取用户信息
        setIsLoggedIn(true);
        setUser({ userId, tenantId } as User);
      } else {
        setUser(null);
        setIsLoggedIn(false);
      }
    } catch (error) {
      console.error('加载用户数据失败:', error);
      setUser(null);
      setIsLoggedIn(false);
    } finally {
      setLoading(false);
    }
  };

  // 保存用户数据到本地存储
  const saveUserToStorage = (token: string, userInfo: User) => {
    try {
      Taro.setStorageSync('access_token', token);
      Taro.setStorageSync('User', userInfo);
      Taro.setStorageSync('UserId', userInfo.userId);
      Taro.setStorageSync('TenantId', userInfo.tenantId);
      Taro.setStorageSync('Phone', userInfo.phone);
    } catch (error) {
      console.error('保存用户数据失败:', error);
    }
  };

  // 登录用户
  const loginUser = (token: string, userInfo: User) => {
    setUser(userInfo);
    setIsLoggedIn(true);
    saveUserToStorage(token, userInfo);
  };

  // 退出登录
  const logoutUser = () => {
    setUser(null);
    setIsLoggedIn(false);

    // 清除本地存储
    try {
      Taro.removeStorageSync('access_token');
      Taro.removeStorageSync('User');
      Taro.removeStorageSync('UserId');
      Taro.removeStorageSync('TenantId');
      Taro.removeStorageSync('Phone');
      Taro.removeStorageSync('userInfo');
    } catch (error) {
      console.error('清除用户数据失败:', error);
    }
  };

  // 从服务器获取最新用户信息
  const fetchUserInfo = async () => {
    if (!isLoggedIn) {
      return null;
    }

    try {
      setLoading(true);
      const userInfo = await getUserInfo();
      setUser(userInfo);

      // 更新本地存储
      const token = Taro.getStorageSync('access_token');
      if (token) {
        saveUserToStorage(token, userInfo);
      }

      return userInfo;
    } catch (error) {
      console.error('获取用户信息失败:', error);
      // 如果获取失败，可能是token过期，清除登录状态
      const errorMessage = error instanceof Error ? error.message : String(error);
      if (errorMessage?.includes('401') || errorMessage?.includes('未授权')) {
        logoutUser();
      }
      return null;
    } finally {
      setLoading(false);
    }
  };

  // 更新用户信息
  const updateUser = async (userData: Partial<User>) => {
    if (!user) {
      throw new Error('用户未登录');
    }

    try {
      const updatedUser = { ...user, ...userData };
      await updateUserInfo(updatedUser);

      setUser(updatedUser);

      // 更新本地存储
      const token = Taro.getStorageSync('access_token');
      if (token) {
        saveUserToStorage(token, updatedUser);
      }

      Taro.showToast({
        title: '更新成功',
        icon: 'success',
        duration: 1500
      });

      return updatedUser;
    } catch (error) {
      console.error('更新用户信息失败:', error);
      Taro.showToast({
        title: '更新失败',
        icon: 'error',
        duration: 1500
      });
      throw error;
    }
  };

  // 检查是否有特定权限
  const hasPermission = (permission: string) => {
    if (!user || !user.authorities) {
      return false;
    }
    return user.authorities.some(auth => auth.authority === permission);
  };

  // 检查是否有特定角色
  const hasRole = (roleCode: string) => {
    if (!user || !user.roles) {
      return false;
    }
    return user.roles.some(role => role.roleCode === roleCode);
  };

  // 获取用户头像URL
  const getAvatarUrl = () => {
    return user?.avatar || user?.avatarUrl || '';
  };

  const getUserId = () => {
    return user?.userId;
  };

  // 获取用户显示名称
  const getDisplayName = () => {
    return user?.nickname || user?.realName || user?.username || '未登录';
  };

  // 获取用户显示的角色（同步版本）
  const getRoleName = () => {
    if(hasRole('superAdmin')){
      return '超级管理员';
    }
    if(hasRole('admin')){
      return '管理员';
    }
    if(hasRole('staff')){
      return '员工';
    }
    if(hasRole('vip')){
      return 'VIP会员';
    }
    return '注册用户';
  }

  // 检查用户是否已实名认证
  const isCertified = () => {
    return user?.certification === true;
  };

  // 检查用户是否是管理员
  const isAdmin = () => {
    return user?.isAdmin === true;
  };

  const isSuperAdmin = () => {
    return user?.isSuperAdmin === true;
  };

  // 获取用户余额
  const getBalance = () => {
    return user?.balance || 0;
  };

  // 获取用户积分
  const getPoints = () => {
    return user?.points || 0;
  };

  // 初始化时加载用户数据
  useEffect(() => {
    loadUserFromStorage();
  }, []);

  return {
    // 状态
    user,
    isLoggedIn,
    loading,

    // 方法
    loginUser,
    logoutUser,
    fetchUserInfo,
    updateUser,
    loadUserFromStorage,

    // 工具方法
    hasPermission,
    hasRole,
    getAvatarUrl,
    getDisplayName,
    getRoleName,
    isCertified,
    isAdmin,
    getBalance,
    getPoints,
    getUserId,
    isSuperAdmin
  };
};
