// ignore_for_file: library_private_types_in_public_api, avoid_web_libraries_in_flutter, deprecated_member_use

import 'dart:async';
import 'package:didvan/utils/extension.dart';

import 'package:record/record.dart';
import 'package:universal_html/html.dart' as html;
import 'dart:io';
import 'package:audio_session/audio_session.dart';
import 'package:didvan/config/design_config.dart';
import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/models/ai/bots_model.dart';
import 'package:didvan/models/ai/chats_model.dart';
import 'package:didvan/models/ai/files_model.dart';
import 'package:didvan/models/ai/messages_model.dart';
import 'package:didvan/models/ai/ai_model_enum.dart';
import 'package:didvan/services/media/media.dart';
import 'package:didvan/utils/action_sheet.dart';
import 'package:didvan/utils/date_time.dart';
import 'package:didvan/views/ai/ai_chat_state.dart';
import 'package:didvan/views/ai/widgets/audio_wave.dart';
import 'package:didvan/views/ai/widgets/message_bar_btn.dart';
import 'package:didvan/views/widgets/animated_visibility.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:didvan/views/widgets/marquee_text.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_sound/flutter_sound.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:persian_number_utility/persian_number_utility.dart';
import 'package:provider/provider.dart';
import 'package:flutter_svg/flutter_svg.dart';

typedef _Fn = void Function();

class AiMessageBar extends StatefulWidget {
  final BotsModel bot;
  final String? assistantsName;
  final bool? attch;
  final bool showSearchToggle;
  final bool isSearchMode;
  final ValueChanged<bool>? onSearchModeToggled;
  final bool showModelSelector;
  final AiModel selectedModel;
  final ValueChanged<AiModel>? onModelChanged;

  const AiMessageBar({
    Key? key,
    required this.bot,
    this.attch,
    this.assistantsName,
    this.showSearchToggle = false,
    this.isSearchMode = false,
    this.onSearchModeToggled,
    this.showModelSelector = false,
    this.selectedModel = AiModel.chatGPT,
    this.onModelChanged,
  }) : super(key: key);

  @override
  _AiMessageBarState createState() => _AiMessageBarState();
}

class _AiMessageBarState extends State<AiMessageBar> {
  FlutterSoundPlayer? _mPlayer = FlutterSoundPlayer();
  FlutterSoundRecorder? _mRecorder = FlutterSoundRecorder();

  Codec _codec = Codec.aacMP4;
  String _mPath = '${DateTime.now().millisecondsSinceEpoch ~/ 1000}.mp4';

  bool _mPlayerIsInited = false;
  bool _mRecorderIsInited = false;
  bool _mplaybackReady = false;
  late bool openAttach = widget.attch ?? false;

  Timer? _timer;
  final theSource = AudioSource.microphone;
  final ValueNotifier<Duration> _countTimer = ValueNotifier(Duration.zero);

  @override
  void initState() {
    _mPlayer!.openPlayer().then((value) {
      setState(() {
        _mPlayerIsInited = true;
      });
    });

    openTheRecorder().then((value) {
      setState(() {
        _mRecorderIsInited = true;
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    _mPlayer!.closePlayer();
    _mPlayer = null;

    _mRecorder!.closeRecorder();
    _mRecorder = null;
    _timer?.cancel();
    super.dispose();
  }

  Future<void> openTheRecorder() async {
    if (!kIsWeb) {
      var status = await Permission.microphone.status;
      await AudioRecorder().hasPermission();
      if (status != PermissionStatus.granted) {
        if (!Platform.isIOS) {
          throw RecordingPermissionException(
              'Microphone permission not granted');
        }
      }
    }
    await _mRecorder!.openRecorder();
    if (!await _mRecorder!.isEncoderSupported(_codec) && kIsWeb) {
      _codec = Codec.opusWebM;
      _mPath = '${DateTime.now().millisecondsSinceEpoch ~/ 1000}.webm';
      if (!await _mRecorder!.isEncoderSupported(_codec) && kIsWeb) {
        _mRecorderIsInited = true;
        return;
      }
    }
    final session = await AudioSession.instance;
    await session.configure(AudioSessionConfiguration(
      avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
      avAudioSessionCategoryOptions:
          AVAudioSessionCategoryOptions.allowBluetooth |
              AVAudioSessionCategoryOptions.defaultToSpeaker,
      avAudioSessionMode: AVAudioSessionMode.spokenAudio,
      avAudioSessionRouteSharingPolicy:
          AVAudioSessionRouteSharingPolicy.defaultPolicy,
      avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.none,
      androidAudioAttributes: const AndroidAudioAttributes(
        contentType: AndroidAudioContentType.speech,
        flags: AndroidAudioFlags.none,
        usage: AndroidAudioUsage.voiceCommunication,
      ),
      androidAudioFocusGainType: AndroidAudioFocusGainType.gain,
      androidWillPauseWhenDucked: true,
    ));

    _mRecorderIsInited = true;
  }

  void startTimer() {
    const oneSec = Duration(seconds: 1);
    _timer = Timer.periodic(
      oneSec,
      (Timer timer) {
        _countTimer.value = Duration(seconds: _countTimer.value.inSeconds + 1);
      },
    );
  }

  void record() async {
    _countTimer.value = Duration.zero;

    await _mRecorder!
        .startRecorder(
      toFile: _mPath,
      codec: _codec,
      audioSource: theSource,
    )
        .then((value) {
      setState(() {
        startTimer();
      });
    });
  }

  void stopRecorder() async {
    await _mRecorder!.stopRecorder().then((value) {
      setState(() {
        var url = value;
        final state = context.read<AiChatState>();
        state.file = FilesModel(url!,
            audio: true, isRecorded: true, duration: _countTimer.value);
        _mplaybackReady = true;
        _timer?.cancel();
      });
    });
    _mPlayer!.setSubscriptionDuration(_countTimer.value);
  }

  void play() async {
    assert(_mPlayerIsInited &&
        _mplaybackReady &&
        _mRecorder!.isStopped &&
        _mPlayer!.isStopped);
    _mPlayer!
        .startPlayer(
            fromURI: _mPath,
            whenFinished: () {
              setState(() {});
            })
        .then((value) {
      setState(() {});
    });
  }

  void stopPlayer() {
    _mPlayer!.stopPlayer().then((value) {
      setState(() {});
    });
  }

  void pausePlayer() {
    _mPlayer!.pausePlayer().then((value) {
      setState(() {});
    });
  }

  void resumePlayer() {
    _mPlayer!.resumePlayer().then((value) {
      setState(() {});
    });
  }

  _Fn? getRecorderFn() {
    if (!_mRecorderIsInited || !_mPlayer!.isStopped) {
      return null;
    }
    return _mRecorder!.isStopped ? record : stopRecorder;
  }

  _Fn? getPlaybackFn() {
    if (!_mPlayerIsInited || !_mplaybackReady || !_mRecorder!.isStopped) {
      return null;
    }
    return _mPlayer!.isPlaying
        ? pausePlayer
        : _mPlayer!.isPaused
            ? resumePlayer
            : _mPlayer!.isStopped
                ? play
                : stopPlayer;
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<AiChatState>(builder: (context, state, child) {
      final theme = Theme.of(context);

      return Container(
        padding: const EdgeInsets.fromLTRB(12, 24, 12, 0).copyWith(
            top: (state.file != null && !state.file!.isRecorded) ? 0 : 24),
        decoration: BoxDecoration(
          color: theme.colorScheme.background,
        ),
        child: Column(
          children: [
            if (state.file != null && !(state.file!.isRecorded))
              fileContainer(),
            Stack(
              children: [
                Container(
                  decoration: BoxDecoration(
                    color: const Color.fromARGB(255, 245, 245, 245),
                    border: Border.all(
                        color: const Color.fromARGB(255, 0, 126, 167),
                        width: 1.5),
                    borderRadius: BorderRadius.circular(16),
                  ),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          recorderAndSendButton(state),
                          if (!(_mRecorder!.isStopped))
                            ValueListenableBuilder(
                              valueListenable: _countTimer,
                              builder: (context, value, child) => Padding(
                                padding: const EdgeInsets.fromLTRB(8, 0, 8, 8),
                                child: SizedBox(
                                  width: 50,
                                  child: Center(
                                    child: DidvanText(
                                        DateTimeUtils.normalizeTimeDuration(
                                            value)),
                                  ),
                                ),
                              ),
                            ),
                          Expanded(
                            child: Padding(
                              padding: _mRecorder!.isPaused ||
                                      _mRecorder!.isRecording
                                  ? const EdgeInsets.fromLTRB(12, 8, 0, 8)
                                  : (widget.showSearchToggle ||
                                          widget.showModelSelector)
                                      ? EdgeInsets.zero.copyWith(top: 4.0)
                                      : EdgeInsets.zero,
                              child:
                                  recorderAndTextMessageHandler(context, state),
                            ),
                          ),
                        ],
                      ),
                      if (widget.showSearchToggle || widget.showModelSelector)
                        Padding(
                          padding: const EdgeInsets.only(
                              top: 4.0, left: 12.0, right: 12.0, bottom: 8.0),
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              if (widget.showModelSelector)
                                Builder(
                                  builder: (context) {
                                    String icon;
                                    String label;

                                    switch (widget.selectedModel) {
                                      case AiModel.gemini:
                                        icon =
                                            'lib/assets/icons/Google-gemini-icon.svg';
                                        label = 'Gemini';
                                        break;
                                      case AiModel.grok:
                                        icon = 'lib/assets/icons/grok.svg';
                                        label = 'Grok';
                                        break;
                                      case AiModel.chatGPT:
                                      default:
                                        icon =
                                            'lib/assets/icons/ChatGPT-Logo.svg';
                                        label = 'ChatGPT';
                                    }

                                    return PopupMenuButton<AiModel>(
                                      onSelected: (AiModel model) {
                                        widget.onModelChanged?.call(model);
                                      },
                                      offset: const Offset(0, 40),
                                      shape: RoundedRectangleBorder(
                                        borderRadius:
                                            BorderRadius.circular(12.0),
                                      ),
                                      itemBuilder: (BuildContext context) => [
                                        PopupMenuItem<AiModel>(
                                          value: AiModel.chatGPT,
                                          child: Row(
                                            children: [
                                              SvgPicture.asset(
                                                'lib/assets/icons/ChatGPT-Logo.svg',
                                                height: 17,
                                              ),
                                              const SizedBox(width: 12.0),
                                              const DidvanText(
                                                'ChatGPT',
                                                fontSize: 14,
                                              ),
                                            ],
                                          ),
                                        ),
                                        PopupMenuItem<AiModel>(
                                          value: AiModel.gemini,
                                          child: Row(
                                            children: [
                                              SvgPicture.asset(
                                                'lib/assets/icons/Google-gemini-icon.svg',
                                                height: 17,
                                              ),
                                              const SizedBox(width: 12.0),
                                              const DidvanText(
                                                'Gemini',
                                                fontSize: 14,
                                              ),
                                            ],
                                          ),
                                        ),
                                        PopupMenuItem<AiModel>(
                                          value: AiModel.grok,
                                          child: Row(
                                            children: [
                                              SvgPicture.asset(
                                                'lib/assets/icons/grok.svg',
                                                height: 17,
                                              ),
                                              const SizedBox(width: 12.0),
                                              const DidvanText(
                                                'Grok',
                                                fontSize: 14,
                                              ),
                                            ],
                                          ),
                                        ),
                                      ],
                                      child: Container(
                                        padding: const EdgeInsets.symmetric(
                                            horizontal: 10.0, vertical: 6.0),
                                        decoration: BoxDecoration(
                                          borderRadius:
                                              BorderRadius.circular(100.0),
                                        ),
                                        child: Row(
                                          mainAxisSize: MainAxisSize.min,
                                          children: [
                                            SvgPicture.asset(
                                              icon,
                                              height: 17,
                                            ),
                                            const SizedBox(width: 8.0),
                                            DidvanText(
                                              label,
                                              fontSize: 14,
                                              color: const Color.fromARGB(
                                                  255, 61, 61, 61),
                                            ),
                                            const SizedBox(width: 4.0),
                                            Center(
                                              child: SvgPicture.asset(
                                                'lib/assets/icons/eva_arrow-down-fill.svg',
                                                height: 18,
                                              ),
                                            )
                                          ],
                                        ),
                                      ),
                                    );
                                  },
                                ),
                              if (widget.showSearchToggle &&
                                  widget.showModelSelector)
                                const SizedBox(width: 8.0),
                              if (widget.showSearchToggle)
                                Builder(
                                  builder: (context) {
                                    const Color activeColor =
                                        Color.fromARGB(255, 0, 126, 167);
                                    final bool isSearchMode =
                                        widget.isSearchMode;
                                    final Color backgroundColor = isSearchMode
                                        ? activeColor.withOpacity(0.1)
                                        : theme.colorScheme.surface;

                                    return InkWell(
                                      onTap: () {
                                        widget.onSearchModeToggled
                                            ?.call(!widget.isSearchMode);
                                      },
                                      borderRadius:
                                          BorderRadius.circular(100.0),
                                      child: Container(
                                        padding: const EdgeInsets.symmetric(
                                            horizontal: 10.0, vertical: 6.0),
                                        decoration: BoxDecoration(
                                          color: backgroundColor,
                                          borderRadius:
                                              BorderRadius.circular(100.0),
                                        ),
                                        child: Row(
                                          mainAxisSize: MainAxisSize.min,
                                          children: [
                                            SvgPicture.asset(
                                              'lib/assets/icons/global-search 2.svg',
                                              height: 25,
                                            ),
                                            const SizedBox(width: 8.0),
                                            const DidvanText(
                                              'جستجو در وب',
                                              fontSize: 14,
                                              color: Color.fromARGB(
                                                  255, 61, 61, 61),
                                            ),
                                          ],
                                        ),
                                      ),
                                    );
                                  },
                                ),
                            ],
                          ),
                        ),
                    ],
                  ),
                ),
                if (state.onResponsing)
                  Positioned.fill(
                    child: Container(
                      decoration: BoxDecoration(
                          color: theme.colorScheme.focused.withOpacity(0.5)),
                    ),
                  )
              ],
            ),
            MediaQuery.of(context).viewInsets.bottom == 0
                ? const Padding(
                    padding: EdgeInsets.fromLTRB(3, 8, 3, 4),
                    child: DidvanText(
                      'مدل‌های هوش مصنوعی می‌توانند اشتباه کنند، صحت اطلاعات مهم را بررسی کنید و از وارد کردن اطلاعات حساس بپرهیزید.',
                      fontSize: 10,
                      fontWeight: FontWeight.normal,
                      color: Color.fromARGB(255, 95, 95, 95),
                    ),
                  )
                : const SizedBox(
                    height: 12,
                  )
          ],
        ),
      );
    });
  }

  Widget recorderAndTextMessageHandler(
      BuildContext context, AiChatState state) {
    return _mRecorder!.isPaused
        ? Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Expanded(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: List.generate(
                      32,
                      (index) => Padding(
                            padding: const EdgeInsets.symmetric(
                                horizontal: 1.0, vertical: 12),
                            child: Container(
                              width: 3,
                              height: 8,
                              decoration: BoxDecoration(
                                  color: Theme.of(context)
                                      .colorScheme
                                      .primary
                                      .withOpacity(0.4)),
                            ),
                          )),
                ),
              ),
              MessageBarBtn(
                enable: true,
                icon: DidvanIcons.play_regular,
                click: () async {
                  await _mRecorder?.resumeRecorder();
                  setState(() {
                    startTimer();
                  });
                },
              )
            ],
          )
        : _mRecorder!.isRecording
            ? Row(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Expanded(
                      child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: List.generate(
                              5,
                              (index) => SpinKitWave(
                                    color: Theme.of(context)
                                        .colorScheme
                                        .primary
                                        .withOpacity(0.4),
                                    size: 32,
                                    itemCount: 10,
                                  )))),
                  MessageBarBtn(
                    enable: true,
                    icon: DidvanIcons.pause_regular,
                    click: () async {
                      await _mRecorder?.pauseRecorder();

                      setState(() {
                        _timer!.cancel();
                      });
                    },
                  )
                ],
              )
            : Row(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Expanded(
                    child: state.file != null && state.file!.isRecorded
                        ? audioContainer()
                        : ValueListenableBuilder(
                            valueListenable: state.message,
                            builder: (context, message, child) {
                              return Directionality(
                                textDirection:
                                    message.text.toString().startsWithEnglish()
                                        ? TextDirection.ltr
                                        : TextDirection.rtl,
                                child: edittext(context, state),
                              );
                            }),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(bottom: 8.0),
                    child: Row(children: [
                      attachmentLayout(state, context),
                      if (widget.bot.attachmentType!.isNotEmpty)
                        Padding(
                          padding: const EdgeInsets.fromLTRB(12, 0, 12, 0),
                          child: MessageBarBtn(
                            enable: true,
                            icon: openAttach || state.file != null
                                ? DidvanIcons.close_regular
                                : Icons.add,
                            click: () {
                              if (_mPlayer!.isPlaying) {
                                stopPlayer();
                              }
                              if (state.file != null) {
                                state.file = null;
                              } else {
                                openAttach = !openAttach;
                              }
                              state.update();
                            },
                          ),
                        )
                    ]),
                  )
                ],
              );
  }

  TextFormField edittext(BuildContext context, AiChatState state) {
    return TextFormField(
      textInputAction: TextInputAction.newline,
      style: Theme.of(context).textTheme.bodyMedium,
      minLines: 1,
      maxLines: 6,
      keyboardType: TextInputType.multiline,
      controller: state.message,
      enabled: true,
      decoration: InputDecoration(
        contentPadding: const EdgeInsets.fromLTRB(12, 12, 0, 15),
        border: InputBorder.none,
        hintText: 'بنویسید...',
        hintStyle: Theme.of(context)
            .textTheme
            .bodySmall!
            .copyWith(color: const Color.fromARGB(255, 95, 95, 95)),
        suffixIcon: state.isEdite
            ? InkWell(
                onTap: () {
                  state.isEdite = false;
                  state.update();
                },
                child: const Icon(DidvanIcons.close_circle_solid),
              )
            : null,
      ),
    );
  }

  recorderAndSendButton(AiChatState state) {
    return ValueListenableBuilder(
      valueListenable: state.message,
      builder: (context, message, child) => Padding(
        padding: const EdgeInsets.fromLTRB(12, 0, 12, 8),
        child: message.text.isEmpty &&
                widget.bot.attachmentType!.contains('audio') &&
                state.file == null &&
                widget.bot.attachment != 0
            ? MessageBarBtn(
                enable: true,
                iconWidget: _mRecorder!.isRecording || _mRecorder!.isPaused
                    ? SvgPicture.asset(
                        'lib/assets/icons/microphone-3.svg',
                        width: 24,
                        height: 24,
                      )
                    : SvgPicture.asset(
                        'lib/assets/icons/microphone-3.svg',
                        width: 29,
                        height: 29,
                      ),
                click: getRecorderFn(),
              )
            : MessageBarBtn(
                enable: (state.file != null && state.file!.isRecorded) ||
                    (widget.bot.attachment == 1) ||
                    message.text.isNotEmpty,
                iconWidget: SvgPicture.asset(
                  'lib/assets/icons/send3.svg',
                  width: 24,
                  height: 24,
                  color: const Color.fromARGB(255, 0, 126, 167),
                ),
                click: () async {
                  if ((state.file == null || !state.file!.isRecorded) &&
                      (widget.bot.attachment != 1) &&
                      message.text.isEmpty) {
                    return;
                  }

                  if (state.messages.isNotEmpty &&
                      DateTime.parse(state.messages.last.dateTime)
                          .toPersianDateStr()
                          .contains(DateTime.parse(DateTime.now()
                                  .subtract(const Duration(minutes: 210))
                                  .toIso8601String())
                              .toPersianDateStr())) {
                    state.messages.last.prompts.add(Prompts(
                      error: false,
                      text: message.text.replaceAll(r"\n", r" \n \n "),
                      // file: state.file?.path,
                      // fileName: state.file?.basename,
                      fileLocal: state.file,
                      finished: true,
                      role: 'user',
                      createdAt: DateTime.now()
                          .subtract(const Duration(minutes: 210))
                          .toIso8601String(),
                    ));
                  } else {
                    state.messages.add(MessageModel(
                        dateTime: DateTime.now()
                            .subtract(const Duration(minutes: 210))
                            .toIso8601String(),
                        prompts: [
                          Prompts(
                            error: false,
                            text: message.text.replaceAll(r"\n", r" \n \n "),
                            finished: true,
                            // file: state.file?.path,
                            // fileName: state.file?.basename,
                            fileLocal: state.file,
                            role: 'user',
                            createdAt: DateTime.now()
                                .subtract(const Duration(minutes: 210))
                                .toIso8601String(),
                          )
                        ]));
                  }
                  state.message.clear();
                  openAttach = false;
                  state.update();
                  await state.postMessage(
                      widget.bot, widget.assistantsName != null);
                },
              ),
      ),
    );
  }

  AnimatedVisibility attachmentLayout(AiChatState state, BuildContext context) {
    return AnimatedVisibility(
        isVisible: openAttach,
        fadeMode: FadeMode.horizontal,
        duration: DesignConfig.lowAnimationDuration,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            if (widget.bot.attachmentType!.contains('pdf'))
              Padding(
                padding: const EdgeInsets.only(right: 8.0),
                child: MessageBarBtn(
                  enable: true,
                  icon: CupertinoIcons.doc_fill,
                  click: () async {
                    MediaService.onLoadingPickFile(context);
                    FilePickerResult? result = await MediaService.pickPdfFile();
                    if (result != null) {
                      String? name = result.files.single.name;

                      if (kIsWeb) {
                        Uint8List? bytes = result.files.first.bytes;

                        state.file = FilesModel(
                          '',
                          name: name,
                          bytes: bytes,
                          audio: false,
                          image: false,
                        );
                      } else {
                        state.file = FilesModel(result.files.single.path!,
                            audio: false, image: false, name: name);
                      }
                    }
                    Future.delayed(
                      Duration.zero,
                      () => ActionSheetUtils(context).pop(),
                    );
                    openAttach = !openAttach;

                    state.update();
                  },
                ),
              ),
            if (widget.bot.attachmentType!.contains('image'))
              Padding(
                padding: const EdgeInsets.only(right: 8.0),
                child: MessageBarBtn(
                  enable: true,
                  icon: CupertinoIcons.photo,
                  click: () async {
                    MediaService.onLoadingPickFile(context);

                    final pickedFile = await MediaService.pickImage(
                        source: ImageSource.gallery);
                    CroppedFile? file;
                    if (pickedFile != null && !kIsWeb) {
                      file = await ImageCropper().cropImage(
                        sourcePath: pickedFile.path,
                        uiSettings: [
                          AndroidUiSettings(toolbarTitle: 'برش تصویر'),
                          IOSUiSettings(
                            title: 'برش تصویر',
                            doneButtonTitle: 'تایید',
                            cancelButtonTitle: 'بازگشت',
                          )
                        ],
                        compressQuality: 30,
                      );

                      if (file == null) {
                        await Future.delayed(
                          Duration.zero,
                          () => ActionSheetUtils(context).pop(),
                        );

                        return;
                      }
                    }
                    if (pickedFile == null) {
                      await Future.delayed(
                        Duration.zero,
                        () => ActionSheetUtils(context).pop(),
                      );

                      return;
                    }
                    state.file = kIsWeb
                        ? FilesModel(pickedFile.path,
                            name: pickedFile.name, image: true, audio: false)
                        : FilesModel(file!.path,
                            name: pickedFile.name, image: true, audio: false);
                    await Future.delayed(
                      Duration.zero,
                      () => ActionSheetUtils(context).pop(),
                    );
                    openAttach = !openAttach;

                    state.update();
                  },
                ),
              ),
            // if (!kIsWeb && !Platform.isIOS)
            if (widget.bot.attachmentType!.contains('audio'))
              Padding(
                padding: const EdgeInsets.only(right: 8.0),
                child: MessageBarBtn(
                  enable: true,
                  icon: CupertinoIcons.music_note_2,
                  click: () async {
                    MediaService.onLoadingPickFile(context);

                    FilePickerResult? result =
                        await MediaService.pickAudioFile();
                    if (result != null) {
                      String? name = result.files.first.name;

                      if (kIsWeb) {
                        Uint8List? bytes = result.files.first.bytes;

                        final blob = html.Blob([bytes]);
                        final blobUrl = html.Url.createObjectUrlFromBlob(blob);

                        state.file = FilesModel(
                          blobUrl,
                          name: name,
                          bytes: bytes,
                          audio: true,
                          image: false,
                        );
                      } else {
                        state.file = FilesModel(result.files.single.path!,
                            name: name, audio: true, image: false);
                      }
                    }
                    await Future.delayed(
                      Duration.zero,
                      () => ActionSheetUtils(context).pop(),
                    );
                    openAttach = !openAttach;

                    state.update();
                  },
                ),
              )
          ],
        ));
  }

  Widget audioContainer() {
    final state = context.watch<AiChatState>();

    return SizedBox(
      width: MediaQuery.sizeOf(context).width,
      child: AudioWave(
        file: state.file!.path,
        loadingPaddingSize: 8.0,
        totalDuration: _countTimer.value,
      ),
    );
  }

  Widget fileContainer() {
    final state = context.watch<AiChatState>();
    return Container(
      decoration: BoxDecoration(
        borderRadius: DesignConfig.mediumBorderRadius,
        color: Theme.of(context).colorScheme.border,
      ),
      margin: const EdgeInsets.fromLTRB(4, 4, 4, 8),
      padding: const EdgeInsets.fromLTRB(12, 8, 12, 8),
      child: Row(
        children: [
          state.file != null && state.file!.isImage()
              ? SizedBox(
                  width: 32,
                  height: 42,
                  child: ClipRRect(
                      borderRadius: DesignConfig.lowBorderRadius,
                      child: state.file!.isNetwork()
                          ? Image.network(
                              state.file!.path,
                              fit: BoxFit.cover,
                            )
                          : Image.file(
                              state.file!.main,
                              fit: BoxFit.cover,
                            )))
              : const Icon(Icons.file_copy),
          const SizedBox(
            width: 12,
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: 24,
                  child: MarqueeText(
                    text: state.file != null ? state.file!.name ?? '' : '',
                    style: const TextStyle(fontSize: 14),
                    stop: const Duration(seconds: 3),
                  ),
                ),
                if (state.file != null && !kIsWeb)
                  FutureBuilder(
                      future: state.file!.main.length(),
                      builder: (context, snapshot) {
                        if (!snapshot.hasData) {
                          return const SizedBox();
                        }
                        return DidvanText(
                          'File Size ${(snapshot.data! / 1000).round()} KB',
                          fontSize: 12,
                        );
                      })
              ],
            ),
          )
        ],
      ),
    );
  }
}
