import 'package:cargocalculaterapp/core/extension/build_context_extension.dart';
import 'package:cargocalculaterapp/core/utils/app_utils.dart';
import 'package:cargocalculaterapp/core/widgets/loading/progress_hud.dart';
import 'package:cargocalculaterapp/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../../core/widgets/rating/rating_bar_widget.dart';
import '../../../../../core/widgets/text_filds/custom_text_field_name.dart';
import '../../bloc/comment/comment_bloc.dart';
import '../../mixin/comment_mixin.dart';
import '../arguments/comment_argument.dart';

class CommentPage extends StatefulWidget {
  const CommentPage({super.key, required this.argument});

  final CommentArgument? argument;

  @override
  State<CommentPage> createState() => _CommentPageState();
}

class _CommentPageState extends State<CommentPage> with CommentMixin {
  @override
  void initState() {
    initControllers();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CommentBloc, CommentState>(
      builder: (context, state) {
        return Scaffold(
          appBar: AppBar(title: Text(AppLocalization.current.left_comment)),
          body: ModalProgressHUD(
            inAsyncCall: state.isLoading,
            child: ListView(
              padding: AppUtils.kPaddingAll16,
              children: [
                Text(
                  AppLocalization.current.comment_desc,
                  style: context.text.authDesc,
                ),
                AppUtils.kBoxHeight16,
                RatingBarWidget(
                  rating: state.rating,
                  separator: AppUtils.kBoxWith12,
                  iconsSize: 24,
                  onRatingChanged: (rating) {
                    context.read<CommentBloc>().add(
                      RatingChangedEvent(rating: rating),
                    );
                  },
                ),
                AppUtils.kBoxHeight24,
                CustomTextFieldName(
                  name: AppLocalization.current.comment,
                  hint: AppLocalization.current.comment_text,
                  minLines: 4,
                  controller: commentController,
                  onchange: (comment) {
                    context.read<CommentBloc>().add(
                      CommentEnterEvent(comment: comment),
                    );
                  },
                ),
                AppUtils.kBoxHeight16,
                Padding(
                  padding: AppUtils.kPaddingHor34,
                  child: ElevatedButton(
                    onPressed:
                        state.rating > 0 && (state.comment?.isNotEmpty ?? false)
                        ? () {
                            context.read<CommentBloc>().add(
                              SubmitEvent(
                                orderId: widget.argument?.orderId ?? "",
                              ),
                            );
                          }
                        : null,
                    child: Text(AppLocalization.current.send),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

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