import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ProfileScreen extends StatefulWidget {
  @override
  State<ProfileScreen> createState() => _ProfileScreenState();
}

class _ProfileScreenState extends State<ProfileScreen> {
  final _nicknameController = TextEditingController();
  String? _selectedCity;
  List<String> _selectedInterests = [];

  final _cities = ['北京', '上海', '广州', '深圳', '成都', '重庆'];
  final _interests = ['太极', '晨练', '书法', '摄影', '舞蹈', '旅游', '茶艺', '手工', '唱歌', '棋牌'];

  @override
  void initState() {
    super.initState();
    _loadProfile();
  }

  void _loadProfile() async {
    final prefs = await SharedPreferences.getInstance();
    setState(() {
      _nicknameController.text = prefs.getString('nickname') ?? '';
      _selectedCity = prefs.getString('city');
      _selectedInterests = prefs.getStringList('interests') ?? [];
    });
  }

  void _save() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('nickname', _nicknameController.text);
    if (_selectedCity != null) await prefs.setString('city', _selectedCity!);
    await prefs.setStringList('interests', _selectedInterests);
    
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('保存成功')),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('个人资料')),
      body: ListView(
        padding: EdgeInsets.all(16),
        children: [
          TextField(
            controller: _nicknameController,
            decoration: InputDecoration(
              labelText: '昵称',
              border: OutlineInputBorder(),
            ),
          ),
          SizedBox(height: 20),
          DropdownButtonFormField<String>(
            value: _selectedCity,
            decoration: InputDecoration(
              labelText: '居住城市',
              border: OutlineInputBorder(),
            ),
            items: _cities.map((city) => DropdownMenuItem(value: city, child: Text(city))).toList(),
            onChanged: (val) => setState(() => _selectedCity = val),
          ),
          SizedBox(height: 20),
          Text('兴趣爱好（最多5个）', style: TextStyle(fontSize: 16)),
          SizedBox(height: 10),
          Wrap(
            spacing: 8,
            children: _interests.map((interest) {
              final selected = _selectedInterests.contains(interest);
              return FilterChip(
                label: Text(interest),
                selected: selected,
                onSelected: (val) {
                  setState(() {
                    if (val && _selectedInterests.length < 5) {
                      _selectedInterests.add(interest);
                    } else {
                      _selectedInterests.remove(interest);
                    }
                  });
                },
              );
            }).toList(),
          ),
          SizedBox(height: 40),
          ElevatedButton(
            onPressed: _save,
            style: ElevatedButton.styleFrom(
              backgroundColor: Color(0xFF333333),
              foregroundColor: Colors.white,
              padding: EdgeInsets.symmetric(vertical: 16),
            ),
            child: Text('保存', style: TextStyle(fontSize: 16)),
          ),
        ],
      ),
    );
  }
}
