
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:persistent_bottom_nav_bar_v2/persistent_bottom_nav_bar_v2.dart';
import 'package:provider/provider.dart';

import '../../../../api/marianumcloud/files-sharing/fileSharingApi.dart';
import '../../../../api/marianumcloud/files-sharing/fileSharingApiParams.dart';
import '../../../../api/marianumcloud/talk/sendMessage/sendMessage.dart';
import '../../../../api/marianumcloud/talk/sendMessage/sendMessageParams.dart';
import '../../../../api/marianumcloud/webdav/webdavApi.dart';
import '../../../../model/chatList/chatProps.dart';
import '../../../../storage/base/settingsProvider.dart';
import '../../../../widget/filePick.dart';
import '../../../../widget/focusBehaviour.dart';
import '../../files/filesUploadDialog.dart';

class ChatTextfield extends StatefulWidget {
  final String sendToToken;
  const ChatTextfield(this.sendToToken, {super.key});

  @override
  State<ChatTextfield> createState() => _ChatTextfieldState();
}

class _ChatTextfieldState extends State<ChatTextfield> {
  late SettingsProvider settings;
  final TextEditingController _textBoxController = TextEditingController();
  bool isLoading = false;

  void _query() {
    Provider.of<ChatProps>(context, listen: false).run();
  }

  void share(String shareFolder, List<String> filePaths) {
    for (var element in filePaths) {
      var fileName = element.split(Platform.pathSeparator).last;
      FileSharingApi().share(FileSharingApiParams(
          shareType: 10,
          shareWith: widget.sendToToken,
          path: '$shareFolder/$fileName',
      )).then((value) => _query());
    }
  }

  Future<void> mediaUpload(List<String>? paths) async {
    if (paths == null) return;

    var shareFolder = 'MarianumMobile';
    WebdavApi.webdav.then((webdav) {
      webdav.mkcol(PathUri.parse('/$shareFolder'));
    });

    pushScreen(
      context,
      withNavBar: false,
      screen: FilesUploadDialog(
        filePaths: paths,
        remotePath: shareFolder,
        onUploadFinished: (uploadedFilePaths) {
          share(shareFolder, uploadedFilePaths);
        },
        uniqueNames: true,
      ),
    );
  }

  void setDraft(String text) {
    if(text.isNotEmpty) {
      settings.val(write: true).talkSettings.drafts[widget.sendToToken] = text;
    } else {
      settings.val(write: true).talkSettings.drafts.removeWhere((key, value) => key == widget.sendToToken);
    }
  }

  @override
  void initState() {
    super.initState();
    settings = Provider.of<SettingsProvider>(context, listen: false);
  }

  @override
  Widget build(BuildContext context) {
    _textBoxController.text = settings.val().talkSettings.drafts[widget.sendToToken] ?? '';

    return Stack(
      children: <Widget>[
        Align(
          alignment: Alignment.bottomLeft,
          child: Container(
            padding: const EdgeInsets.only(left: 10, bottom: 3, top: 3, right: 10),
            width: double.infinity,
            child: Row(
              children: <Widget>[
                GestureDetector(
                  onTap: (){
                    showDialog(context: context, builder: (context) => SimpleDialog(
                        children: [
                          ListTile(
                            leading: const Icon(Icons.file_open),
                            title: const Text('Aus Dateien auswählen'),
                            onTap: () {
                              FilePick.documentPick().then(mediaUpload);
                              Navigator.of(context).pop();
                            },
                          ),
                          Visibility(
                            visible: !Platform.isIOS,
                            child: ListTile(
                              leading: const Icon(Icons.image),
                              title: const Text('Aus Gallerie auswählen'),
                              onTap: () {
                                FilePick.multipleGalleryPick().then((value) {
                                  if(value != null) mediaUpload(value.map((e) => e.path).toList());
                                });
                                Navigator.of(context).pop();
                              },
                            ),
                          ),
                        ],
                      ));
                  },
                  child: Material(
                    elevation: 5,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(30),
                    ),
                    child: Container(
                      height: 30,
                      width: 30,
                      decoration: BoxDecoration(
                        color: Theme.of(context).primaryColor,
                        borderRadius: BorderRadius.circular(30),
                      ),
                      child: const Icon(Icons.attach_file_outlined, color: Colors.white, size: 20, ),
                    ),
                  )
                ),
                const SizedBox(width: 15),
                Expanded(
                  child: TextField(
                    autocorrect: true,
                    textCapitalization: TextCapitalization.sentences,
                    controller: _textBoxController,
                    maxLines: 7,
                    minLines: 1,
                    decoration: const InputDecoration(
                      hintText: 'Nachricht schreiben...',
                      border: InputBorder.none,
                    ),
                    onChanged: (String text) {
                      if(text.trim().toLowerCase() == 'marbot marbot marbot') {
                        var newText = 'Roboter sind cool und so, aber Marbots sind besser!';
                        _textBoxController.text = newText;
                        text = newText;
                      }
                      setDraft(text);
                    },
                    onTapOutside: (PointerDownEvent event) => FocusBehaviour.textFieldTapOutside(context),
                  ),
                ),
                const SizedBox(width: 15),
                FloatingActionButton(
                  mini: true,
                  onPressed: (){
                    if(_textBoxController.text.isEmpty) return;
                    if(isLoading) return;

                    setState(() {
                      isLoading = true;
                    });
                    SendMessage(widget.sendToToken, SendMessageParams(_textBoxController.text)).run().then((value) {
                      _query();
                      setState(() {
                        isLoading = false;
                      });
                      _textBoxController.text = '';
                      setDraft('');
                    });
                  },
                  backgroundColor: Theme.of(context).primaryColor,
                  elevation: 5,
                  child: isLoading
                      ? Container(padding: const EdgeInsets.all(10), child: const CircularProgressIndicator(color: Colors.white, strokeWidth: 2))
                      : const Icon(Icons.send, color: Colors.white, size: 18),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
