import React, { useState, useEffect } from 'react';
import { View, Text, Image, ScrollView, Button } from '@tarojs/components';
import Taro from '@tarojs/taro';
import './index.scss';

// 用户信息接口
interface UserInfo {
  id: string;
  nickname: string;
  avatar: string;
  join_date: string;
  stats: {
    following: number;
    friends: number;
    hosted: number;
    participated: number;
  };
  tags: string[];
  bio: string;
  location: string;
  occupation: string;
  ntrp_level: string;
}

// 球局记录接口
interface GameRecord {
  id: string;
  title: string;
  date: string;
  time: string;
  duration: string;
  location: string;
  type: string;
  distance: string;
  participants: {
    avatar: string;
    nickname: string;
  }[];
  max_participants: number;
  current_participants: number;
  level_range: string;
  game_type: string;
  images: string[];
}

const MyselfPage: React.FC = () => {
  // 获取页面参数
  const instance = Taro.getCurrentInstance();
  const user_id = instance.router?.params?.userid;
  
  // 判断是否为当前用户
  const is_current_user = !user_id;
  
  // 模拟用户数据
  const [user_info] = useState<UserInfo>({
    id: '1',
    nickname: '188的王晨',
    avatar: require('../../../static/userInfo/default_avatar.svg'),
    join_date: '2025年9月加入',
    stats: {
      following: 124,
      friends: 24,
      hosted: 7,
      participated: 24
    },
    tags: ['上海黄浦', '互联网从业者', 'NTRP 4.0'],
    bio: '网球入坑两年，偏好双打，正手进攻型选手\n平时在张江、世纪公园附近活动，欢迎约球！\n不卷分数，但认真对待每一拍，每一场球都想打得开心。有时候也会带相机来拍点照片📸',
    location: '上海黄浦',
    occupation: '互联网从业者',
    ntrp_level: 'NTRP 4.0'
  });

  // 模拟球局数据
  const [game_records] = useState<GameRecord[]>([
    {
      id: '1',
      title: '女生轻松双打',
      date: '明天(周五)',
      time: '下午5点',
      duration: '2小时',
      location: '仁恒河滨花园网球场',
      type: '室外',
      distance: '3.5km',
      participants: [
        { avatar: require('../../../static/userInfo/user1.svg'), nickname: '用户1' },
        { avatar: require('../../../static/userInfo/user2.svg'), nickname: '用户2' }
      ],
      max_participants: 4,
      current_participants: 2,
      level_range: '2.0 至 2.5',
      game_type: '双打',
      images: [
        require('../../../static/userInfo/game1.svg'),
        require('../../../static/userInfo/game2.svg'),
        require('../../../static/userInfo/game3.svg')
      ]
    }
  ]);

  // 关注状态
  const [is_following, setIsFollowing] = useState(false);

  // 当前激活的标签页
  const [active_tab, setActiveTab] = useState<'hosted' | 'participated'>('hosted');

  // 处理关注/取消关注
  const handle_follow = () => {
    setIsFollowing(!is_following);
    Taro.showToast({
      title: is_following ? '已取消关注' : '关注成功',
      icon: 'success',
      duration: 1500
    });
  };

  // 处理分享
  const handle_share = () => {
    Taro.showShareMenu({
      withShareTicket: true
    });
  };

  // 处理返回
  const handle_back = () => {
    Taro.navigateBack();
  };

  // 处理编辑资料
  const handle_edit_profile = () => {
    Taro.navigateTo({
      url: '/pages/userInfo/edit/index'
    });
  };

  // 处理球局详情
  const handle_game_detail = (game_id: string) => {
    Taro.navigateTo({
      url: `/pages/game/detail/index?id=${game_id}`
    });
  };

  // 处理球局订单
  const handle_game_orders = () => {
    Taro.navigateTo({
      url: '/pages/game/orders/index'
    });
  };

  // 处理收藏
  const handle_favorites = () => {
    Taro.navigateTo({
      url: '/pages/game/favorites/index'
    });
  };

  return (
    <View className="myself_page">
      {/* 主要内容 */}
      <ScrollView className="main_content" scrollY>
        {/* 用户信息区域 */}
        <View className="user_info_section">
          {/* 头像和基本信息 */}
          <View className="basic_info">
            <View className="avatar_container">
              <Image className="avatar" src={user_info.avatar} />
            </View>
            <View className="info_container">
              <Text className="nickname">{user_info.nickname}</Text>
              <Text className="join_date">{user_info.join_date}</Text>
            </View>
          </View>

          {/* 统计数据 */}
          <View className="stats_section">
            <View className="stats_container">
              <View className="stat_item">
                <Text className="stat_number">{user_info.stats.following}</Text>
                <Text className="stat_label">关注</Text>
              </View>
              <View className="stat_item">
                <Text className="stat_number">{user_info.stats.friends}</Text>
                <Text className="stat_label">球友</Text>
              </View>
              <View className="stat_item">
                <Text className="stat_number">{user_info.stats.hosted}</Text>
                <Text className="stat_label">主办</Text>
              </View>
              <View className="stat_item">
                <Text className="stat_number">{user_info.stats.participated}</Text>
                <Text className="stat_label">参加</Text>
              </View>
            </View>
            <View className="action_buttons">
              {/* 只有非当前用户才显示关注按钮 */}
              {!is_current_user && (
                <Button 
                  className={`follow_button ${is_following ? 'following' : ''}`}
                  onClick={handle_follow}
                >
                  <Image 
                    className="button_icon" 
                    src={require('../../../static/userInfo/plus.svg')} 
                  />
                  <Text className="button_text">
                    {is_following ? '已关注' : '关注'}
                  </Text>
                </Button>
              )}
              {/* 只有非当前用户才显示消息按钮 */}
              {!is_current_user && (
                <Button className="message_button">
                  <Image 
                    className="button_icon" 
                    src={require('../../../static/userInfo/message.svg')} 
                  />
                </Button>
              )}
              {/* 只有当前用户才显示编辑按钮 */}
              {is_current_user && (
                <Button className="edit_button" onClick={handle_edit_profile}>
                  <Text className="button_text">编辑</Text>
                </Button>
              )}
              {/* 只有当前用户才显示分享按钮 */}
              {is_current_user && (
                <Button className="share_button" onClick={handle_share}>
                  <Text className="button_text">分享</Text>
                </Button>
              )}
            </View>
          </View>

          {/* 标签和简介 */}
          <View className="tags_bio_section">
            <View className="tags_container">
              <View className="tag_item">
                <Image 
                  className="tag_icon" 
                  src={require('../../../static/userInfo/location.svg')} 
                />
                <Text className="tag_text">{user_info.location}</Text>
              </View>
              <View className="tag_item">
                <Text className="tag_text">{user_info.occupation}</Text>
              </View>
              <View className="tag_item">
                <Text className="tag_text">{user_info.ntrp_level}</Text>
              </View>
            </View>
            <Text className="bio_text">{user_info.bio}</Text>
          </View>

          {/* 球局订单和收藏功能 */}
          <View className="quick_actions_section">
            <View className="action_card">
              <View className="action_content" onClick={handle_game_orders}>
                <Image 
                  className="action_icon" 
                  src={require('../../../static/userInfo/tennis.svg')} 
                />
                <Text className="action_text">球局订单</Text>
              </View>
              <View className="action_divider"></View>
              <View className="action_content" onClick={handle_favorites}>
                <Image 
                  className="action_icon" 
                  src={require('../../../static/userInfo/tennis.svg')} 
                />
                <Text className="action_text">收藏</Text>
              </View>
            </View>
          </View>
        </View>

        {/* 球局类型标签页 */}
        <View className="game_tabs_section">
          <View className="tab_container">
            <View className={`tab_item ${active_tab === 'hosted' ? 'active' : ''}`} onClick={() => setActiveTab('hosted')}>
              <Text className="tab_text">我主办的</Text>
            </View>
            <View className={`tab_item ${active_tab === 'participated' ? 'active' : ''}`} onClick={() => setActiveTab('participated')}>
              <Text className="tab_text">我参与的</Text>
            </View>
          </View>
        </View>

        {/* 球局列表 */}
        <View className="game_list_section">
          <View className="date_header">
            <Text className="date_text">5月28日</Text>
            <Text className="separator">/</Text>
            <Text className="weekday_text">星期三</Text>
          </View>
          
          {/* 球局卡片 */}
          <View className="game_cards">
            {game_records.map((game) => (
              <View 
                key={game.id} 
                className="game_card"
                onClick={() => handle_game_detail(game.id)}
              >
                {/* 球局标题和类型 */}
                <View className="game_header">
                  <Text className="game_title">{game.title}</Text>
                  <View className="game_type_icon">
                    <Image 
                      className="type_icon" 
                      src={require('../../../static/userInfo/tennis.svg')} 
                    />
                  </View>
                </View>

                {/* 球局时间 */}
                <View className="game_time">
                  <Text className="time_text">
                    {game.date} {game.time} {game.duration}
                  </Text>
                </View>

                {/* 球局地点和类型 */}
                <View className="game_location">
                  <Text className="location_text">{game.location}</Text>
                  <Text className="separator">·</Text>
                  <Text className="type_text">{game.type}</Text>
                  <Text className="separator">·</Text>
                  <Text className="distance_text">{game.distance}</Text>
                </View>

                {/* 球局图片 */}
                <View className="game_images">
                  {game.images.map((image, index) => (
                    <Image 
                      key={index} 
                      className="game_image" 
                      src={image} 
                    />
                  ))}
                </View>

                {/* 球局信息标签 */}
                <View className="game_tags">
                  <View className="participants_info">
                    <View className="avatars">
                      {game.participants.map((participant, index) => (
                        <Image 
                          key={index} 
                          className="participant_avatar" 
                          src={participant.avatar} 
                        />
                      ))}
                    </View>
                    <View className="participants_count">
                      <Text className="count_text">
                        报名人数 {game.current_participants}/{game.max_participants}
                      </Text>
                    </View>
                  </View>
                  <View className="game_info_tags">
                    <View className="info_tag">
                      <Text className="tag_text">{game.level_range}</Text>
                    </View>
                    <View className="info_tag">
                      <Text className="tag_text">{game.game_type}</Text>
                    </View>
                  </View>
                </View>
              </View>
            ))}
          </View>
        </View>
      </ScrollView>
    </View>
  );
};

export default MyselfPage;
