// ignore_for_file: deprecated_member_use

import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/constants/assets.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/providers/server_data.dart';
import 'package:didvan/services/media/voice.dart';
import 'package:didvan/views/direct/direct_state.dart';
import 'package:didvan/views/direct/widgets/message.dart';
import 'package:didvan/views/direct/widgets/message_box.dart';
import 'package:didvan/views/widgets/didvan/icon_button.dart';
import 'package:didvan/views/widgets/didvan/scaffold.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:didvan/views/widgets/logos/didvan_vertical_logo.dart';
import 'package:didvan/views/widgets/state_handlers/empty_state.dart';
import 'package:didvan/views/widgets/state_handlers/sliver_state_handler.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';

class Direct extends StatefulWidget {
  final Map<String, dynamic> pageData;
  const Direct({Key? key, required this.pageData}) : super(key: key);

  @override
  State<Direct> createState() => _DirectState();
}

class _DirectState extends State<Direct> {
  @override
  void initState() {
    final state = context.read<DirectState>();
    state.mPlayer!.openPlayer().then((value) {
      state.mPlayerIsInited = true;
    });

    state.openTheRecorder().then((value) {
      state.mRecorderIsInited = true;
    });

    state.replyNews = widget.pageData['newsAttachment'];
    state.replyRadar = widget.pageData['radarAttachment'];
    final typeId = ServerDataProvider.labelToTypeId(widget.pageData['type']);
    state.typeId = typeId;
    Future.delayed(Duration.zero, () {
      state.getMessages();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final state = context.watch<DirectState>();
    final d = MediaQuery.of(context);
    return WillPopScope(
      onWillPop: () async {
        VoiceService.resetVoicePlayer();
        return true;
      },
      child: Material(
        child: Stack(
          children: [
            Positioned(
              top: 60 + d.padding.top + 56,
              bottom: 56,
              left: 0,
              right: 0,
              child: DidvanScaffold(
                hidePlayer: true,
                padding: EdgeInsets.zero,
                reverse: true,
                backgroundColor: Theme.of(context).colorScheme.surface,
                appBarData: null,
                slivers: [
                  if (state.appState != AppState.busy)
                    SliverPadding(
                      padding:
                          state.replyRadar == null && state.replyNews == null
                              ? EdgeInsets.zero
                              : const EdgeInsets.only(bottom: 68),
                      sliver: SliverStateHandler<DirectState>(
                        itemPadding: const EdgeInsets.only(bottom: 12),
                        state: state,
                        enableEmptyState: state.messages.isEmpty,
                        emptyState: Padding(
                          padding: const EdgeInsets.only(bottom: 160),
                          child: EmptyState(
                            asset: Assets.emptyChat,
                            title: 'اولین پیام را بنویسید...',
                          ),
                        ),
                        builder: (context, state, index) => Message(
                          message: state.messages[index],
                        ),
                        childCount: state.messages.length,
                        onRetry: state.getMessages,
                      ),
                    ),
                ],
                children: [
                  if (state.appState == AppState.busy)
                    SizedBox(
                      height: d.size.height - kToolbarHeight - d.padding.top,
                      child: Center(
                        child: SpinKitSpinningLines(
                          color: Theme.of(context).colorScheme.primary,
                        ),
                      ),
                    ),
                ],
              ),
            ),
            Positioned(
              top: 0,
              right: 20,
              child: Container(
                height: 60 + d.padding.top,
                padding: EdgeInsets.only(top: d.padding.top),
                child: const DidvanHorizontalLogo(),
              ),
            ),
            Positioned(
              top: 60 + d.padding.top,
              left: 0,
              right: 0,
              child: Container(
                height: 56,
                color: Theme.of(context).colorScheme.surface,
                child: Row(
                  children: [
                    const SizedBox(width: 16),
                    Expanded(
                      child: Text(
                        widget.pageData['type'] ?? 'پیام به پشتیبانی',
                        style: Theme.of(context)
                            .textTheme
                            .headlineSmall
                            ?.copyWith(
                                color: const Color.fromARGB(255, 0, 53, 70),
                                fontWeight: FontWeight.bold,
                                fontSize: 19),
                      ),
                    ),
                    IconButton(
                      onPressed: () => Navigator.of(context).pop(),
                      icon: SvgPicture.asset(
                        'lib/assets/icons/arrow-left.svg',
                        width: 30,
                        height: 30,
                        colorFilter: const ColorFilter.mode(
                            Color.fromARGB(255, 102, 102, 102),
                            BlendMode.srcIn),
                      ),
                    ),
                    const SizedBox(width: 8),
                  ],
                ),
              ),
            ),
            Positioned(
              bottom: d.viewInsets.bottom,
              right: 0,
              left: 0,
              child: const MessageBox(),
            ),
            if (state.deletionQueue.isNotEmpty)
              Positioned(
                left: 0,
                right: 0,
                top: d.padding.top,
                child: Container(
                  height: 72,
                  color: Theme.of(context).colorScheme.surface,
                  child: Row(
                    children: [
                      DidvanIconButton(
                        icon: DidvanIcons.close_solid,
                        size: 32,
                        gestureSize: 48,
                        color: Theme.of(context).colorScheme.secondary,
                        onPressed: () {
                          state.deletionQueue.clear();
                          state.update();
                        },
                      ),
                      DidvanText(
                        '${state.deletionQueue.length} مورد انتخاب شد',
                        style: Theme.of(context).textTheme.titleMedium,
                        color: Theme.of(context).colorScheme.secondary,
                      ),
                      const Spacer(),
                      DidvanIconButton(
                        icon: DidvanIcons.trash_solid,
                        size: 32,
                        gestureSize: 48,
                        color: Theme.of(context).colorScheme.primary,
                        onPressed: state.delete,
                      ),
                    ],
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }
}
