import 'package:farm_tpf/presentation/custom_widgets/app_bar_widget.dart';
import 'package:farm_tpf/presentation/custom_widgets/button_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:keyboard_dismisser/keyboard_dismisser.dart';

import '../../../models/item_dropdown.dart';
import '../../../utils/utils.dart';
import '../../custom_widgets/date_picker/date_picker_widget.dart';
import '../../custom_widgets/dropdown/dropdown_bottom_sheet.dart';
import '../../custom_widgets/textfield/text_field_normal.dart';
import 'cubit/update_activity_cubit.dart';
import 'widgets/item_column.dart';

class UpdateActivityPage extends StatefulWidget {
  final String stampCode;
  const UpdateActivityPage({super.key, required this.stampCode});

  @override
  State<UpdateActivityPage> createState() => _UpdateActivityPageState();
}

class _UpdateActivityPageState extends State<UpdateActivityPage> {
  final bloc = UpdateActivityCubit();

  @override
  void initState() {
    super.initState();
    bloc.preparedData();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget(),
      body: BlocListener<UpdateActivityCubit, UpdateActivityState>(
        bloc: bloc,
        listener: ((context, state) {
          if (state is UpdateActivityLoading) {
            SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
              UtilWidget.showLoading();
            });
          } else if (state is UpdateActivityFailure) {
            SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
              UtilWidget.hideLoading();
              // UtilWidget.showToastError(state.errorMessage);
            });
          } else if (state is UpdateActivityPrepareDataSuccessful) {
            SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
              UtilWidget.hideLoading();
            });
          }
        }),
        child: KeyboardDismisser(
          child: Container(
            child: Form(
              key: bloc.formKey,
              child: Column(
                children: [
                  Expanded(
                    child: _widgetBody(),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: ButtonWidget(
                      title: 'Cập nhật',
                      onPressed: () {
                        bloc.onSubmit(widget.stampCode);
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _widgetBody() {
    return Container(
      padding: const EdgeInsets.all(16),
      child: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            ItemColumnWidget(
              title: 'Hoạt động',
              child: ValueListenableBuilder<String>(
                  valueListenable: bloc.selectedActivityType,
                  builder: (context, selected, _) {
                    return ValueListenableBuilder<List<ItemDropDown>>(
                      valueListenable: bloc.activityTypes,
                      builder: (context, types, _) {
                        return DropdownBottomSheet(
                          dataSources: types,
                          initValue: selected,
                          onSelected: (val) {
                            bloc.selectedActivityType.value = val.key ?? '';
                          },
                          hint: 'Hoạt động',
                        );
                      },
                    );
                  }),
            ),
            const SizedBox(
              height: 8,
            ),
            ItemColumnWidget(
              title: 'Mô tả',
              child: TextFieldNormal(
                controller: bloc.descriptionCtl,
                maxLines: 1,
                hint: 'Mô tả',
              ),
            ),
            SizedBox(
              height: 8,
            ),
            ValueListenableBuilder<DateTime>(
              valueListenable: bloc.actionDate,
              builder: (context, actionDate, _) {
                return ItemColumnWidget(
                  title: 'Ngày cập nhập',
                  child: DatePickerWidget(
                    initDateTime: actionDate,
                    onUpdateDateTime: (selectedDate) {
                      if (selectedDate != null) {
                        bloc.actionDate.value = selectedDate;
                      }
                    },
                  ),
                );
              },
            ),
            SizedBox(
              height: 8,
            ),
            ItemColumnWidget(
              title: 'Vị trí',
              child: TextFieldNormal(
                controller: bloc.locationCtl,
                maxLines: 1,
                hint: 'Vị trí',
              ),
            ),
            const SizedBox(
              height: 16,
            ),
          ],
        ),
      ),
    );
  }
}
