import 'dart:async';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:lottie/lottie.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/config/routes.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/ui/marathon/marathon_provider.dart';
import 'package:mohem_flutter_app/ui/marathon/widgets/custom_status_widget.dart';
import 'package:mohem_flutter_app/ui/marathon/widgets/marathon_header.dart';
import 'package:provider/provider.dart';

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

  @override
  Widget build(BuildContext context) {
    MarathonProvider provider = context.read<MarathonProvider>();
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: [
            const MarathonHeader(),
            20.height,
            QualifiersContainer(provider: provider)
                .paddingOnly(left: 21, right: 21),
            20.height,
            InkWell(
              onTap: () {
                Navigator.pushNamed(context, AppRoutes.marathonWinnerScreen);
              },
              child: Container(
                height: 50,
                decoration: BoxDecoration(
                  color: MyColors.greenColor,
                  borderRadius: BorderRadius.circular(15),
                  boxShadow: [
                    BoxShadow(
                      color: const Color(0xff000000).withOpacity(.05),
                      blurRadius: 26,
                      offset: const Offset(0, -3),
                    ),
                  ],
                ),
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 20),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      "Muhammad Shrouff"
                          .toText18(isBold: true, color: MyColors.white),
                      "837436".toText18(isBold: true, color: MyColors.white),
                    ],
                  ),
                ),
              ).paddingOnly(left: 20, right: 20),
            ),
            CustomStatusWidget(
                asset: Lottie.asset(
                  MyLottieConsts.winnerLottie,
                  height: 168,
                ),
                title: Text(
                  LocaleKeys.fingersCrossed.tr(),
                  style: const TextStyle(
                    height: 23 / 24,
                    color: MyColors.greenColor,
                    fontSize: 27,
                    letterSpacing: -1,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                subTitle: Text(
                  LocaleKeys.winnerSelectedRandomly.tr(),
                  textAlign: TextAlign.center,
                  style: const TextStyle(
                    color: MyColors.grey77Color,
                    fontSize: 16,
                    letterSpacing: -0.64,
                    fontWeight: FontWeight.w600,
                  ),
                )).paddingOnly(left: 21, right: 21, top: 20, bottom: 20),
          ],
        ),
      ),
    );
  }
}

class QualifiersContainer extends StatefulWidget {
  final MarathonProvider provider;

  const QualifiersContainer({Key? key, required this.provider})
      : super(key: key);

  @override
  State<QualifiersContainer> createState() => _QualifiersContainerState();
}

class _QualifiersContainerState extends State<QualifiersContainer> {
  final int totalQuestions = 10;

  final int currentQuestion = 04;

  @override
  void initState() {
    scheduleMicrotask(() {
      widget.provider.startTimer(context);
    });
    super.initState();
  }

  @override
  void dispose() {
    widget.provider.cancelTimer();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      decoration: MyDecorations.shadowDecoration,
      padding: const EdgeInsets.symmetric(vertical: 15, horizontal: 20),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              LocaleKeys.winnerSelection
                  .tr()
                  .toText18(isBold: true, color: MyColors.grey3AColor),
              "00:${widget.provider.start < 10 ? "0${widget.provider.start}" : widget.provider.start}"
                  .toText18(isBold: true, color: MyColors.redColor),
            ],
          ),
          10.height,
          Row(
            children: [
              "18 ".toText32(color: MyColors.greenColor),
              LocaleKeys.qualifiers.tr().toText20(color: MyColors.greenColor),
            ],
          ),
        ],
      ),
    );
  }
}
