import 'package:didvan/constants/assets.dart';
import 'package:didvan/models/view/app_bar_data.dart';
import 'package:didvan/views/home/settings/direct_list/direct_list_state.dart';
import 'package:didvan/views/home/settings/direct_list/widgets/direct_item.dart';
import 'package:didvan/views/widgets/didvan/badge.dart';
import 'package:didvan/views/widgets/didvan/divider.dart';
import 'package:didvan/views/widgets/didvan/scaffold.dart';
import 'package:didvan/views/widgets/shimmer_placeholder.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:provider/provider.dart';

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

  @override
  State<DirectList> createState() => _DirectListState();
}

class _DirectListState extends State<DirectList> {
  @override
  void initState() {
    Future.delayed(Duration.zero, () {
      context.read<DirectListState>().getDirectsList();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<DirectListState>(
      builder: (context, state, child) => DidvanScaffold(
        appBarData: AppBarData(
          hasBack: true,
          title: 'پیام‌ها',
          trailing: state.unreadCount == 0
              ? null
              : DidvanBadge(
                  text: state.unreadCount.toString(),
                ),
        ),
        slivers: [
          SliverStateHandler<DirectListState>(
            onRetry: state.getDirectsList,
            itemPadding: const EdgeInsets.symmetric(horizontal: 16),
            state: state,
            placeholder: const _ChatRoomPlaceholder(),
            builder: (context, state, index) => ChatRoomItem(
              chatRoom: state.chatRooms[index],
            ),
            childCount: state.chatRooms.length,
            enableEmptyState: state.chatRooms.isEmpty,
            emptyState: EmptyState(
              asset: Assets.emptyBookmark,
              title: 'پیامی وجود ندارد',
            ),
          ),
        ],
      ),
    );
  }
}

class _ChatRoomPlaceholder extends StatelessWidget {
  const _ChatRoomPlaceholder({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const ShimmerPlaceholder(
              height: 24,
              width: 24,
            ),
            const SizedBox(width: 12),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: const [
                  ShimmerPlaceholder(
                    height: 24,
                    width: 100,
                  ),
                  SizedBox(height: 12),
                  ShimmerPlaceholder(
                    height: 16,
                    width: 300,
                  ),
                  SizedBox(height: 12),
                  ShimmerPlaceholder(
                    height: 12,
                    width: 80,
                  ),
                ],
              ),
            ),
          ],
        ),
        const DidvanDivider(),
      ],
    );
  }
}
