import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lazy_load_scrollview/lazy_load_scrollview.dart';
import 'package:provider/provider.dart';
import 'package:telnow_mobile_new/src/layouts/web/menu-history/history_item_widget.dart';

import '../../../cubit/menu_history_cubit.dart';
import '../../../cubit/tab_history_cubit.dart';
import '../../../utils/U.dart';
import '../../../utils/extensions.dart';
import '../../../utils/provider.dart';
import '../../components/rate_mission.dart';
import '../../components/template.dart';
import '../../functions/history.dart' hide HistoryTab;
import '../history_detail.dart';

class DoneContainer extends StatefulWidget {
  const DoneContainer({super.key});

  @override
  State<DoneContainer> createState() => _DoneContainerState();
}

class _DoneContainerState extends State<DoneContainer>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  late AnimationController _animationController;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 800),
    )..repeat(reverse: true);
    super.initState();
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    HistoryModule historyModule = Provider.of<HistoryModule>(context, listen: false);
    HistoryFunction historyFunction = HistoryFunction();

    List<dynamic> rating = [
      {'key': 1, 'image': "assets/image/icon/very_dissatisfied.png", 'label': 'disatisfied'.tr()},
      {'key': 2, 'image': "assets/image/icon/dissatisfied.png", 'label': 'lessSatisfied'.tr()},
      {'key': 3, 'image': "assets/image/icon/neutral.png", 'label': 'satisfied'.tr()},
      {'key': 4, 'image': "assets/image/icon/satisfied.png", 'label': 'verySatisfied'.tr()},
      {'key': 5, 'image': "assets/image/icon/very_satisfied.png", 'label': 'reallyPleased'.tr()},
    ];

    return BlocBuilder<DoneCubit, TabState>(
      builder: (context, state) {
        UserModule userModule = Provider.of<UserModule>(context, listen: false);
        var data = state.data;

        if(state.isLoading) {
          return loadingTemplateNoVoid();
        } else if(data.isEmpty){
          return NoDataPage();
        }

        return LazyLoadScrollView(
          onEndOfPage: () => context.read<DoneCubit>().loadNextPage(),
          scrollOffset: 300,
          child: ListView.builder(
            itemCount: data.length,
            itemBuilder: (context, i){
              return HistoryItemWidget(
                tab: 'done',
                state: state,
                index: i,
                item: data[i],
                userModule: userModule,
                rating: rating,
                animationController: _animationController,
              );
            }
          ),
        );
      },
    );
  }
}
