import 'package:appinio_swiper/appinio_swiper.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/decorations_helper.dart';
import 'package:mohem_flutter_app/classes/lottie_consts.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/models/marathon/question_model.dart';
import 'package:mohem_flutter_app/ui/marathon/marathon_provider.dart';
import 'package:provider/provider.dart';

class QuestionCard extends StatelessWidget {
  const QuestionCard({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    MarathonProvider provider = context.read<MarathonProvider>();
    return CupertinoPageScaffold(
      child: provider.cardContentList.isEmpty
          ? Lottie.asset(MyLottieConsts.hourGlassLottie, height: 250).paddingOnly(top: 50)
          : SizedBox(
              height: 440,
              width: double.infinity,
              child: AppinioSwiper(
                duration: const Duration(milliseconds: 400),
                padding: EdgeInsets.zero,
                isDisabled: true,
                controller: provider.swiperController,
                unswipe: (int index, AppinioSwiperDirection direction) {},
                onSwipe: (int index, AppinioSwiperDirection direction) {},
                cards: provider.cardContentList,
              ),
            ),
    );
  }
}

class CardContent extends StatelessWidget {
  const CardContent({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    MarathonProvider provider = context.watch<MarathonProvider>();
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        color: CupertinoColors.white,
        boxShadow: <BoxShadow>[
          BoxShadow(
            color: CupertinoColors.systemGrey.withOpacity(0.2),
            spreadRadius: 3,
            blurRadius: 7,
            offset: const Offset(0, 3),
          )
        ],
      ),
      alignment: Alignment.center,
      child: Column(
        children: <Widget>[
          Container(
            height: 78,
            width: double.infinity,
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                transform: GradientRotation(.83),
                begin: Alignment.topRight,
                end: Alignment.bottomLeft,
                colors: <Color>[
                  MyColors.gradiantEndColor,
                  MyColors.gradiantStartColor,
                ],
              ),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(10),
                topRight: Radius.circular(10),
              ),
            ),
            child: Center(
              child: Padding(
                padding: const EdgeInsets.symmetric(horizontal: 13),
                child: Text(
                  AppState().isArabic(context) ? provider.currentQuestion.titleAr ?? "" : provider.currentQuestion.titleEn ?? "",
                  style: const TextStyle(
                    color: MyColors.white,
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ),
          ),
          const AnswerContent(),
        ],
      ),
    );
  }
}

class AnswerContent extends StatelessWidget {
  const AnswerContent({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    MarathonProvider provider = context.watch<MarathonProvider>();
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 31, horizontal: 13),
      decoration: const BoxDecoration(
        color: MyColors.kWhiteColor,
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(10),
          bottomRight: Radius.circular(10),
        ),
      ),
      child: provider.currentQuestion.questionOptions != null
          ? ListView.separated(
              itemCount: provider.currentQuestion.questionOptions!.length,
              shrinkWrap: true,
              itemBuilder: (BuildContext context, int index) {
                return AnswerTileForText(
                  index: index,
                  onAnswerTapped: () {
                    provider.updateCurrentQuestionOptionStatus(QuestionsOptionStatus.selected, index);
                  },
                );
              },
              separatorBuilder: (BuildContext context, int index) => 15.height,
            )
          : const SizedBox(),
    );
  }
}

class AnswerTileForText extends StatelessWidget {
  final int index;
  final Function() onAnswerTapped;

  const AnswerTileForText({Key? key, required this.index, required this.onAnswerTapped}) : super(key: key);

  Color getAnswerTextColor(QuestionsOptionStatus status) {
    switch (status) {
      case QuestionsOptionStatus.correct:
        return MyColors.white;
      case QuestionsOptionStatus.wrong:
        return MyColors.white;
      case QuestionsOptionStatus.selected:
        return MyColors.white;
      case QuestionsOptionStatus.unSelected:
        return MyColors.darkTextColor;
    }
  }

  @override
  Widget build(BuildContext context) {
    MarathonProvider provider = context.watch<MarathonProvider>();
    return InkWell(
      onTap: () {
        provider.isUserOutOfGame ? Utils.showToast(LocaleKeys.youAreOutOfContest.tr()) : onAnswerTapped();
      },
      child: Container(
        alignment: Alignment.centerLeft,
        decoration: MyDecorations.getAnswersContainerColor(provider.currentQuestion.questionOptions![index].optionStatus!),
        child: Center(
          child: (AppState().isArabic(context) ? provider.currentQuestion.questionOptions![index].titleAr! : provider.currentQuestion.questionOptions![index].titleEn!)
              .toText16(color: provider.isUserOutOfGame ? MyColors.darkTextColor : getAnswerTextColor(provider.currentQuestion.questionOptions![index].optionStatus!))
              .paddingOnly(top: 17, bottom: 17),
        ),
      ),
    );
  }
}
