import 'dart:async';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_countdown_timer/current_remaining_time.dart';
import 'package:flutter_countdown_timer/flutter_countdown_timer.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/ui/marathon/marathon_provider.dart';

class BuildCountdownTimer extends StatelessWidget {
  final int timeToMarathon;
  final MarathonProvider provider;
  final int screenFlag;

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

  final TextStyle styleTextHome = const TextStyle(
    fontSize: 7,
    color: MyColors.greyACColor,
    fontStyle: FontStyle.italic,
    fontWeight: FontWeight.w600,
    letterSpacing: -0.4,
  );

  final TextStyle styleDigitHome = const TextStyle(
    height: 23 / 27,
    color: MyColors.white,
    fontSize: 24,
    fontStyle: FontStyle.italic,
    fontFamily: "Poppins",
    letterSpacing: -1.44,
    fontWeight: FontWeight.bold,
  );

  final TextStyle styleTextMarathon = const TextStyle(
    fontSize: 10,
    fontStyle: FontStyle.normal,
    fontWeight: FontWeight.w600,
    color: MyColors.grey57Color,
    letterSpacing: -0.4,
  );

  final TextStyle styleDigitMarathon = const TextStyle(
    height: 23 / 24,
    color: MyColors.darkTextColor,
    fontSize: 34,
    letterSpacing: -1.44,
    fontWeight: FontWeight.bold,
  );

  Widget buildEmptyWidget() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Column(
          children: <Widget>[
            Text(
              "00",
              style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
            ),
            Text(
              LocaleKeys.days.tr(),
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
        buildSeparator(),
        Column(
          children: <Widget>[
            Text(
              "00",
              style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
            ),
            Text(
              LocaleKeys.hours.tr(),
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
        buildSeparator(),
        Column(
          children: <Widget>[
            Text(
              "00",
              style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
            ),
            Text(
              LocaleKeys.minutes.tr(),
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
        buildSeparator(),
        Column(
          children: <Widget>[
            Text(
              "00",
              style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
            ),
            Text(
              LocaleKeys.seconds.tr(),
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
      ],
    );
  }

  Widget buildSeparator() {
    return Text(
      "  :  ",
      style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
    );
  }

  Widget buildCountdownTimer(CurrentRemainingTime? time) {
    if (time == null) {
      if (!provider.itsMarathonTime) {
        scheduleMicrotask(() {
          provider.itsMarathonTime = true;
        });
      }

      return buildEmptyWidget();
    }
    return Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Column(
          children: <Widget>[
            time.days == null
                ? Text(
                    "00",
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  )
                : Text(
                    time.days! < 10 ? "0${time.days.toString()}" : time.days.toString(),
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  ),
            Text(
              LocaleKeys.days.tr(),
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
        buildSeparator(),
        Column(
          children: <Widget>[
            time.hours == null
                ? Text(
                    "00",
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  )
                : Text(
                    time.hours! < 10 ? "0${time.hours.toString()}" : time.hours.toString(),
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  ),
            Text(
              LocaleKeys.hours.tr(),
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
        buildSeparator(),
        Column(
          children: <Widget>[
            time.min == null
                ? Text(
                    "00",
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  )
                : Text(
                    time.min! < 10 ? "0${time.min.toString()}" : time.min.toString(),
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  ),
            Text(
              LocaleKeys.minutes.tr(),
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
        buildSeparator(),
        Column(
          children: <Widget>[
            time.sec == null
                ? Text(
                    "00",
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  )
                : Text(
                    time.sec! < 10 ? "0${time.sec.toString()}" : time.sec.toString(),
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  ),
            Text(
              LocaleKeys.seconds.tr(),
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return CountdownTimer(
      endTime: timeToMarathon,
      onEnd: null,
      widgetBuilder: (BuildContext context, CurrentRemainingTime? time) {
        return buildCountdownTimer(time);
      },
    );
  }
}
