import 'package:didvan/constants/assets.dart';
import 'package:didvan/models/enums.dart';
import 'package:didvan/models/view/app_bar_data.dart';
import 'package:didvan/providers/server_data_provider.dart';
import 'package:didvan/services/media/media.dart';
import 'package:didvan/views/home/direct/direct_state.dart';
import 'package:didvan/views/home/direct/widgets/message.dart';
import 'package:didvan/views/home/direct/widgets/message_box.dart';
import 'package:didvan/views/widgets/didvan/scaffold.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: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.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 {
        MediaService.resetAudioPlayer();
        return true;
      },
      child: Material(
        child: Stack(
          children: [
            Positioned(
              top: 0,
              bottom: 56,
              left: 0,
              right: 0,
              child: DidvanScaffold(
                reverse: true,
                backgroundColor: Theme.of(context).colorScheme.surface,
                appBarData: AppBarData(
                  hasBack: true,
                  subtitle: 'ارتباط با سردبیر',
                  title: widget.pageData['type'] ?? 'پشتیبانی اپلیکیشن',
                ),
                slivers: [
                  if (state.appState != AppState.busy)
                    SliverPadding(
                      padding: state.replyRadar == 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(
              bottom: d.viewInsets.bottom,
              right: 0,
              left: 0,
              child: const MessageBox(),
            ),
          ],
        ),
      ),
    );
  }
}
