import 'package:didvan/models/view/app_bar_data.dart';
import 'package:didvan/views/home/bookmarks/filtered_bookmark/filtered_bookmarks_state.dart';
import 'package:didvan/views/widgets/overview/multitype.dart';
import 'package:didvan/views/widgets/didvan/scaffold.dart';
import 'package:didvan/views/widgets/state_handlers/empty_list.dart';
import 'package:didvan/views/widgets/state_handlers/sliver_state_handler.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:didvan/views/home/main/widgets/swot_bookmark.dart';

class FilteredBookmarks extends StatefulWidget {
  final void Function(int id)? onDeleted;
  final int type;
  const FilteredBookmarks({Key? key, this.onDeleted, required this.type})
      : super(key: key);

  @override
  State<FilteredBookmarks> createState() => _FilteredBookmarksState();
}

class _FilteredBookmarksState extends State<FilteredBookmarks> {
  @override
  void initState() {
    super.initState();

    final state = context.read<FilteredBookmarksState>();
    Future.delayed(
      Duration.zero,
      () => state.getBookmarks(page: 1),
    );
  }

  String get _appBarTitle {
    switch (widget.type) {
      case 1:
        return 'پویش افق';
      case 2:
        return 'دنیای فولاد';
      case 3:
        return 'ویدیوکست‌ها';
      case 4:
        return 'پادکست‌ها';
      case 5:
        return 'رادارهای استراتژیک';
      case 6:
        return 'سها';
      case 7:
        return 'اینفوگرافی';
      case 8:
        return 'بایدها و نبایدها';
      case 9:
        return 'ماهنامه تحلیلی دیدوان';
      default:
        return 'پویش';
    }
  }

  Future<void> _onBookmarkChanged(
      int id, bool value, bool shouldUpdate, String itemType) async {
    if (value) return;
    widget.onDeleted?.call(id);

    if (itemType == "swot") {
      context.read<FilteredBookmarksState>().onSwotMarkChanged(id);
    } else {
      context.read<FilteredBookmarksState>().onMarkChanged(id, value);
    }
  }

  @override
  Widget build(BuildContext context) {
    final state = context.watch<FilteredBookmarksState>();

    return DidvanScaffold(
      appBarData: AppBarData(title: _appBarTitle, hasBack: true),
      padding: const EdgeInsets.all(16),
      slivers: [
        if (widget.type == 8)
          SliverStateHandler<FilteredBookmarksState>(
            state: state,
            enableEmptyState:
                state.bookmarkedSwotItems.isEmpty && !state.swotItemsLoading,
            emptyState: const EmptyList(),
            placeholder: MultitypeOverview.placeholder,
            builder: (context, state, index) {
              final item = state.bookmarkedSwotItems[index];
              return Padding(
                padding: const EdgeInsets.only(bottom: 8.0),
                child: SwotBookmark(
                  item: item,
                  onSwotUnbookmarked: (postId) {
                    _onBookmarkChanged(postId, false, true, "swot");
                  },
                ),
              );
            },
            itemPadding: EdgeInsets.zero,
            childCount: state.bookmarkedSwotItems.length,
            onRetry: () => state.getBookmarks(page: 1),
          )
        else
          SliverStateHandler<FilteredBookmarksState>(
            state: state,
            enableEmptyState: state.bookmarks.isEmpty,
            itemPadding: const EdgeInsets.only(bottom: 8),
            placeholder: MultitypeOverview.placeholder,
            emptyState: const EmptyList(),
            builder: (context, state, index) {
              if (index >= state.bookmarks.length) {
                if (state.page < state.lastPage) {
                  state.getBookmarks(page: state.page + 1);
                  return MultitypeOverview.placeholder;
                }
                return const SizedBox.shrink();
              }
              final item = state.bookmarks[index];
              return MultitypeOverview(
                item: item,
                enableCaption: true,
                onMarkChanged: (id, value) =>
                    _onBookmarkChanged(id, value, true, item.type),
                enableBookmark: true,
              );
            },
            childCount:
                state.bookmarks.length + (state.page < state.lastPage ? 1 : 0),
            onRetry: () => state.getBookmarks(page: state.page),
          ),
      ],
    );
  }
}
