import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../../../../core/theme/design_tokens.dart';
import '../../../../shared/widgets/page_header.dart' as widgets;
import '../../../../shared/widgets/app_input.dart';

class AddContactScreen extends StatefulWidget {
  final String? contactId;

  const AddContactScreen({super.key, this.contactId});

  @override
  State<AddContactScreen> createState() => _AddContactScreenState();
}

class _AddContactScreenState extends State<AddContactScreen> {
  final _nameController = TextEditingController();
  final _emailController = TextEditingController();
  final _remarkController = TextEditingController();

  bool get isEditing => widget.contactId != null;

  @override
  void dispose() {
    _nameController.dispose();
    _emailController.dispose();
    _remarkController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF8FAFC),
      body: SafeArea(
        child: Column(
          children: [
            widgets.PageHeader(
              leading: widgets.BackButton(),
              trailing: _buildConfirmButton(),
            ),
            Expanded(
              child: SingleChildScrollView(
                padding: const EdgeInsets.fromLTRB(20, 8, 20, 20),
                child: Column(
                  children: [
                    _buildAvatarSection(),
                    const SizedBox(height: 14),
                    _buildFormCard(),
                    if (isEditing) ...[
                      const SizedBox(height: 14),
                      _buildDeleteRow(),
                    ],
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildConfirmButton() {
    return GestureDetector(
      onTap: _handleConfirm,
      child: Container(
        width: 36,
        height: 36,
        decoration: BoxDecoration(
          color: const Color(0xFFEAF3FF),
          borderRadius: BorderRadius.circular(18),
          border: Border.all(color: const Color(0xFFCFE1FB)),
        ),
        child: const Icon(Icons.check, size: 16, color: AppColors.blue600),
      ),
    );
  }

  Widget _buildAvatarSection() {
    return Center(
      child: Container(
        width: 72,
        height: 72,
        decoration: BoxDecoration(
          color: const Color(0xFFF3F6FC),
          borderRadius: BorderRadius.circular(36),
          border: Border.all(color: Colors.transparent),
        ),
        child: const Icon(
          Icons.person_outline,
          size: 24,
          color: AppColors.slate400,
        ),
      ),
    );
  }

  Widget _buildFormCard() {
    return Container(
      padding: const EdgeInsets.all(14),
      decoration: BoxDecoration(
        color: AppColors.white,
        borderRadius: BorderRadius.circular(16),
        border: Border.all(color: const Color(0xFFE3EAF6)),
      ),
      child: Column(
        children: [
          AppInput(label: '昵称', hint: '请输入昵称', controller: _nameController),
          const SizedBox(height: 14),
          AppInput(
            label: '邮箱',
            hint: '请输入邮箱',
            controller: _emailController,
            keyboardType: TextInputType.emailAddress,
          ),
          const SizedBox(height: 14),
          AppInput(
            label: '备注',
            hint: '请输入备注',
            controller: _remarkController,
            maxLines: 3,
          ),
        ],
      ),
    );
  }

  Widget _buildDeleteRow() {
    return GestureDetector(
      onTap: _handleDelete,
      child: Container(
        padding: const EdgeInsets.only(bottom: 8),
        alignment: Alignment.center,
        child: const Text(
          '删除联系人',
          style: TextStyle(
            fontSize: 12,
            fontWeight: FontWeight.w600,
            color: AppColors.red600,
          ),
        ),
      ),
    );
  }

  void _handleConfirm() {
    final name = _nameController.text.trim();
    final email = _emailController.text.trim();

    if (name.isEmpty || email.isEmpty) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('请填写昵称和邮箱')));
      return;
    }

    // TODO: Implement save logic
    context.pop();
  }

  void _handleDelete() {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('删除联系人'),
        content: const Text('确定要删除此联系人吗？'),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('取消'),
          ),
          TextButton(
            onPressed: () {
              Navigator.pop(context);
              // TODO: Implement delete logic
              context.pop();
            },
            child: const Text('删除', style: TextStyle(color: AppColors.red600)),
          ),
        ],
      ),
    );
  }
}
