import 'package:cims_apps/application/assets/path_assets.dart';
import 'package:cims_apps/application/component/button/back_button_view.dart';
import 'package:cims_apps/application/component/button/button_view.dart';
import 'package:cims_apps/application/component/image/image_view.dart';
import 'package:cims_apps/application/theme/color_palette.dart';
import 'package:cims_apps/core/route/route.dart';
import 'package:cims_apps/core/utils/size_config.dart';
import 'package:cims_apps/features/auth/registration/view/submission_data/risk_profile/results_view.dart';
import 'package:cims_apps/features/auth/registration/view/submission_data/risk_profile/risk_profile_view_model/risk_profile_view_model.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

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

  @override
  State<QuestionView> createState() => _QuestionViewState();
}

class _QuestionViewState extends State<QuestionView> {
  int currentPage = 0;
  PageController pageController = PageController();

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => RiskProfileViewModel(),
      child:
          Consumer<RiskProfileViewModel>(builder: (context, provider, child) {
        return Scaffold(
          appBar: AppBar(
            toolbarHeight: 70,
            backgroundColor: Colors.white,
            surfaceTintColor: Colors.white,
            automaticallyImplyLeading: false,
            centerTitle: true,
            title: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                BackButtonView(),
                const Text('Risk Profile', textAlign: TextAlign.center),
                SizedBox(width: SizeConfig.width * 0.1)
              ],
            ),
            shape: const RoundedRectangleBorder(
                side: BorderSide(color: ColorPalette.slate200)),
          ),
          body: PageView(
            controller: pageController,
            physics: NeverScrollableScrollPhysics(),
            children: provider.listRiskProfileQuestion.asMap().entries.map((e) {
              return containerQuestion(e.value, e.key,
                  (index, score) => provider.selectedAnswer(index, score));
            }).toList(),
          ),
          bottomNavigationBar: SizedBox(
            height: 84,
            child: ButtonView(
              disabled: provider.listScore[currentPage] == 0,
              name: 'Next',
              marginVertical: 16,
              disabledBgColor: ColorPalette.slate200,
              textColor: provider.listScore[currentPage] == 0 ? ColorPalette.slate500 : Colors.white,
              backgroundColor: ColorPalette.primary,
              onPressed: () {
                if (currentPage > 3) {
                  int totalScore = provider.listScore
                      .reduce((value, element) => value + element);
                  provider.setTypeResult(totalScore);
                  routePush(context,
                    page: ResultsView(
                        totalScore: totalScore,
                        typeResult: provider.typeResult
                    ),
                    routeType: RouteType.pushReplace
                  );
                } else {
                  setState(() {
                    currentPage += 1;
                  });
                  pageController.nextPage(
                      duration: Duration(milliseconds: 300),
                      curve: Curves.ease);
                }
              },
            ),
          ),
        );
      }),
    );
  }

  Widget containerQuestion(RiskProfileQuestion data, int index,
      void Function(int index, int score) onTapAnswers) {
    return SizedBox(
      height: SizeConfig.height,
      width: SizeConfig.width,
      child: ListView(
        padding: EdgeInsets.all(24),
        children: [
          ImageView(
            image: data.img,
            fit: BoxFit.fitWidth,
          ),
          SizedBox(
            height: 16,
          ),
          Text(
            '${index + 1} of 5 question',
            style: TextStyle(
                fontWeight: FontWeight.bold, color: ColorPalette.primary),
          ),
          SizedBox(
            height: 12,
          ),
          Text(
            data.question,
            style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 24,
                color: ColorPalette.slate800),
          ),
          SizedBox(
            height: 16,
          ),
          Column(
            children: data.answers.asMap().entries.map((e) {
              bool selected = data.selectedScore == e.value.score;
              return GestureDetector(
                onTap: () {
                  onTapAnswers(index, e.value.score);
                },
                child: AnimatedContainer(
                  duration: Duration(milliseconds: 300),
                  margin: EdgeInsets.only(top: 12),
                  padding: EdgeInsets.all(12),
                  decoration: BoxDecoration(
                      border: Border.all(
                          color: selected
                              ? ColorPalette.primary
                              : ColorPalette.slate200),
                      borderRadius: BorderRadius.circular(8)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Expanded(
                        child: Text(
                          e.value.title,
                          style: TextStyle(
                              fontSize: 16,
                              color: selected
                                  ? ColorPalette.primary
                                  : ColorPalette.slate500,
                              fontWeight: FontWeight.w500),
                        ),
                      ),
                      if (selected) ...[
                        Icon(Icons.check_circle_rounded,
                            color: ColorPalette.primary)
                      ]
                    ],
                  ),
                ),
              );
            }).toList(),
          )
        ],
      ),
    );
  }
}
