mirrored_repositories/Flutter-Mobile-Application-Development/flutter_chatbot_using_dialogflow
import 'package:dialog_flowtter/dialog_flowtter.dart';
import 'package:flutter/material.dart';

import 'messages.dart';

class Chat extends StatefulWidget {
  Chat({Key? key}) : super(key: key);

  @override
  State<Chat> createState() => _ChatState();
}

class _ChatState extends State<Chat> {
  late DialogFlowtter dialogFlowtter;
  final TextEditingController _textController = TextEditingController();
  final List<Map<String, dynamic>> _messages = [];

  @override
  void initState() {
    // TODO: implement initState
    DialogFlowtter.fromFile().then((value) => dialogFlowtter = value);
    super.initState();
  }

  sendMessages(String text) async {
    if(text.isEmpty){
      print('Message is empty');
    }else{
      setState(() {
        addMessage(Message(text: DialogText(text: [text])),true);
      });

      DetectIntentResponse response = await dialogFlowtter.detectIntent(queryInput: QueryInput(text: TextInput(text: text)));
      if(response.message == null) return;
      setState(() {
        addMessage(response.message!);
      });
    }
  }

  addMessage(Message message, [bool isUserMessage = false]){
    _messages.add({
      'message': message,
      'isUserMessage': isUserMessage
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.black87,
        title: Text('Chat'),
      ),
      body: Column(
        children: [
          Expanded(
            child: MessagesScreen(messages: _messages),
          ),
          Container(
            padding: EdgeInsets.all(8),
            child: Row(
              children: [
                Expanded(
                  child: TextField(
                    controller: _textController,
                    decoration: InputDecoration(
                      hintText: 'Enter a message',
                    ),
                  ),
                ),
                IconButton(
                  onPressed: () {
                    sendMessages(_textController.text);
                    _textController.clear();
                  },
                  icon: Icon(Icons.send),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
