import 'package:didvan/models/requests/studio.dart';
import 'package:didvan/models/view/app_bar_data.dart';
import 'package:didvan/views/home/settings/bookmarks/filtered_bookmark/filtered_bookmarks_state.dart';
import 'package:didvan/views/home/widgets/overview/news.dart';
import 'package:didvan/views/home/widgets/overview/podcast.dart';
import 'package:didvan/views/home/widgets/overview/radar.dart';
import 'package:didvan/views/home/widgets/overview/video.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';

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

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

class _FilteredBookmarksState extends State<FilteredBookmarks> {
  @override
  void initState() {
    Future.delayed(
      Duration.zero,
      () => context.read<FilteredBookmarksState>().getBookmarks(page: 1),
    );
    super.initState();
  }

  String get _appBarTitle {
    switch (context.read<FilteredBookmarksState>().type) {
      case 'radar':
        return 'تحلیل‌های رادار';
      case 'news':
        return 'اخبار';
      case 'video':
        return 'ویدئو‌ها';
      case 'podcast':
        return 'پادکست‌ها';
      default:
        return 'پادکست‌ها';
    }
  }

  @override
  Widget build(BuildContext context) {
    return DidvanScaffold(
      appBarData: AppBarData(title: _appBarTitle),
      slivers: [
        Consumer<FilteredBookmarksState>(
          builder: (context, state, child) =>
              SliverStateHandler<FilteredBookmarksState>(
            state: state,
            enableEmptyState: state.bookmarks.isEmpty,
            itemPadding: const EdgeInsets.only(bottom: 8),
            placeholder: RadarOverview.placeholder,
            emptyState: const EmptyList(),
            builder: (context, state, index) {
              index++;
              if (index % 15 == 0 && state.lastPage != state.page) {
                state.getBookmarks(page: state.page + 1);
              }
              index--;
              if (state.type == 'radar') {
                return RadarOverview(
                  radar: state.bookmarks[index],
                  onMarkChanged: _onBookmarkChanged,
                  onCommentsChanged: state.onCommentsChanged,
                  hasUnmarkConfirmation: true,
                );
              }
              if (state.type == 'news') {
                return NewsOverview(
                  news: state.bookmarks[index],
                  onMarkChanged: _onBookmarkChanged,
                  hasUnmarkConfirmation: true,
                );
              }
              if (state.type == 'podcast') {
                return PodcastOverview(
                  studioRequestArgs:
                      const StudioRequestArgs(page: 0, type: 'podcast'),
                  podcast: state.bookmarks[index],
                  onMarkChanged: _onBookmarkChanged,
                  hasUnmarkConfirmation: true,
                );
              }
              return VideoOverview(
                studioRequestArgs:
                    const StudioRequestArgs(page: 0, type: 'video'),
                video: state.bookmarks[index],
                onMarkChanged: _onBookmarkChanged,
                hasUnmarkConfirmation: true,
              );
            },
            childCount: state.bookmarks.length,
            onRetry: () => state.getBookmarks(page: state.page),
          ),
        ),
      ],
    );
  }

  Future<void> _onBookmarkChanged(int id, bool value) async {
    if (value) return;
    final state = context.read<FilteredBookmarksState>();
    state.onMarkChanged(id, false);
    widget.onDeleted?.call(id);
  }
}
