import 'package:e_commerce_training/res/R.dart';
import 'package:e_commerce_training/src/model/repository/app_repository.dart';
import 'package:e_commerce_training/src/page/course_feedback/course_feedback_cubit.dart';
import 'package:e_commerce_training/src/page/course_feedback/course_feedback_state.dart';
import 'package:e_commerce_training/src/util/navigation_util.dart';
import 'package:e_commerce_training/src/util/util.dart';
import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CourseFeedbackPage extends StatefulWidget {
  const CourseFeedbackPage({Key? key}) : super(key: key);

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

class _CourseFeedbackPageState extends State<CourseFeedbackPage> {
  TextEditingController commentController = TextEditingController();
  late CourseFeedbackCubit _cubit;

  @override
  void initState() {
    // TODO: implement initState
    AppRepository repository = AppRepository();
    _cubit = CourseFeedbackCubit(repository);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: R.color.grey200,
      body: BlocProvider(
        create: (context) => _cubit,
        child: BlocConsumer<CourseFeedbackCubit, CourseFeedbackState>(
          listener: (context, state) {
            if (state is CourseFeedbackFailure) Util.showErrorSnackBar(context, state.error);
            // TODO: implement listener
          },
          builder: (context, state) {
            return buildPage(context, state);
          },
        ),
      ),
    );
  }

  Widget buildPage(BuildContext context, CourseFeedbackState state) {
    return Stack(
      children: [
        ListView(
          padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
          children: [
            SizedBox(height: 30),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  R.string.evaluation_of_lesson.tr(),
                  style: TextStyle(
                    fontSize: 24.sp,
                    fontWeight: FontWeight.w600,
                    color: R.color.black,
                  ),
                ),
                InkWell(
                  onTap: () {
                    NavigationUtil.pop(context);
                  },
                  child: Icon(
                    Icons.close,
                    size: 30.h,
                    color: R.color.black,
                  ),
                )
              ],
            ),
            SizedBox(height: 20),
            Image.asset(R.drawable.icGolden, height: 200.h, width: 120.h),
            SizedBox(height: 20),
            Text(
              R.string.rate_how_you_feel.tr(),
              style: TextStyle(
                fontSize: 20.sp,
                fontWeight: FontWeight.w400,
                color: R.color.black,
              ),
              maxLines: 2,
              textAlign: TextAlign.center,
            ),
            SizedBox(height: 20),
            Center(
              child: RatingBar.builder(
                itemSize: 60.h,
                initialRating: 0,
                minRating: 0,
                direction: Axis.horizontal,
                allowHalfRating: true,
                itemCount: 5,
                itemPadding: EdgeInsets.symmetric(horizontal: 5),
                itemBuilder: (context, _) => Icon(
                  Icons.star,
                  color: Colors.amber,
                ),
                onRatingUpdate: (rating) {
                  // print(rating);
                },
              ),
            ),
            SizedBox(height: 20),
            Container(
              padding: EdgeInsets.all(20),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(12),
                color: R.color.white,
              ),
              child: Column(
                children: [
                  Row(
                    children: [
                      Icon(
                        Icons.chat_outlined,
                        size: 30.sp,
                        color: R.color.black,
                      ),
                      SizedBox(width: 10),
                      Text(
                        R.string.feelings.tr(),
                        style: TextStyle(
                          fontSize: 22.sp,
                          fontWeight: FontWeight.w600,
                          color: R.color.black,
                        ),
                      ),
                    ],
                  ),
                  TextField(
                    textInputAction: TextInputAction.go,
                    maxLines: 1,
                    expands: false,
                    controller: commentController,
                    decoration: InputDecoration(
                      hintText: R.string.input_feellings.tr(),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
        Positioned(
          left: 100,
          bottom: 10,
          right: 100,
          child: InkWell(
            onTap: () {},
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20),
                color: R.color.primaryColor,
              ),
              child: Text(
                R.string.sent_report.tr(),
                style: TextStyle(
                  fontSize: 20.sp,
                  fontWeight: FontWeight.w600,
                  color: R.color.white,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
