import 'package:ai_chat_assistant/models/chat_message.dart';
import 'package:t_basic_intl/intl.dart';
import 'package:flutter/material.dart';
import '../enums/message_status.dart';
import '../widgets/chat_box.dart';
import '../widgets/gradient_background.dart';
import '../services/message_service.dart';
import 'package:provider/provider.dart';
import '../widgets/chat_header.dart';
import '../widgets/chat_footer.dart';

class FullScreenPage extends StatefulWidget {
  const FullScreenPage({super.key});

  @override
  State<FullScreenPage> createState() => _FullScreenState();
}

class _FullScreenState extends State<FullScreenPage> {
  final ScrollController _scrollController = ScrollController();

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

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  void _scrollToBottom() {
    if (_scrollController.hasClients) {
      _scrollController.animateTo(
        0.0,
        duration: const Duration(milliseconds: 200),
        curve: Curves.easeOut,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GradientBackground(
        colors: const [
          Color(0XFF3B0A3F),
          Color(0xFF0E0E24),
          Color(0xFF0C0B33),
        ],
        child: SafeArea(
          child: Column(
            children: [
              ChatHeader(
                onClose: () {
                  Provider.of<MessageService>(context, listen: false)
                      .abortReply();
                  Navigator.pop(context);
                },
              ),
              const SizedBox(height: 12),
              Expanded(
                child: Consumer<MessageService>(
                  builder: (context, messageService, child) {
                    WidgetsBinding.instance.addPostFrameCallback((_) {
                      _scrollToBottom();
                    });
                    if (messageService.messages.isEmpty) {
                      Future.delayed(const Duration(milliseconds: 500), () {
                        String text = Intl.getCurrentLocale().startsWith('zh')
                            ? "您好，我是众众，请问有什么可以帮您？"
                            : "Hi, I'm Zhongzhong, may I help you ? ";
                        messageService.addMessage(text, false, MessageStatus.normal);
                      });
                    }
                    return ChatBox(
                      scrollController: _scrollController,
                      messages: messageService.messages,
                    );
                  },
                ),
              ),
              const SizedBox(height: 12),
              Consumer<MessageService>(
                builder: (context, messageService, child) => ChatFooter(
                  onClear: () {
                    messageService.abortReply();
                    messageService.clearMessages();
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
