import 'dart:async';

import 'package:auto_size_text/auto_size_text.dart';
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;

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

  final TextStyle styleTextHome = TextStyle(
    color: MyColors.white.withOpacity(0.45),
    fontStyle: FontStyle.italic,
    fontWeight: FontWeight.w800,
    letterSpacing: -0.4,
  );

  final TextStyle styleDigitHome = const TextStyle(
    height: 23 / 27,
    color: MyColors.white,
    fontStyle: FontStyle.italic,
    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>[
            AutoSizeText(
              "00",
              maxFontSize: 24,
              minFontSize: 20,
              style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
            ),
            AutoSizeText(
              LocaleKeys.days.tr(),
              minFontSize: 7,
              maxFontSize: 8,
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
        buildSeparator(),
        Column(
          children: <Widget>[
            AutoSizeText(
              "00",
              maxFontSize: 24,
              minFontSize: 20,
              style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
            ),
            AutoSizeText(
              LocaleKeys.hours.tr(),
              minFontSize: 7,
              maxFontSize: 8,
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
        buildSeparator(),
        Column(
          children: <Widget>[
            AutoSizeText(
              "00",
              maxFontSize: 24,
              minFontSize: 20,
              style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
            ),
            AutoSizeText(
              LocaleKeys.minutes.tr(),
              minFontSize: 7,
              maxFontSize: 8,
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
        buildSeparator(),
        Column(
          children: <Widget>[
            AutoSizeText(
              "00",
              maxFontSize: 24,
              minFontSize: 20,
              style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
            ),
            AutoSizeText(
              LocaleKeys.seconds.tr(),
              minFontSize: 7,
              maxFontSize: 8,
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
      ],
    );
  }

  Widget buildSeparator() {
    return AutoSizeText(
      "  :  ",
      maxFontSize: 24,
      minFontSize: 20,
      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
                ? AutoSizeText(
                    "00",
                    maxFontSize: 24,
                    minFontSize: 20,
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  )
                : AutoSizeText(
                    time.days! < 10 ? "0${time.days.toString()}" : time.days.toString(),
                    maxFontSize: 24,
                    minFontSize: 20,
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  ),
            AutoSizeText(
              LocaleKeys.days.tr(),
              minFontSize: 7,
              maxFontSize: 8,
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
        buildSeparator(),
        Column(
          children: <Widget>[
            time.hours == null
                ? AutoSizeText(
                    "00",
                    maxFontSize: 24,
                    minFontSize: 20,
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  )
                : AutoSizeText(
                    time.hours! < 10 ? "0${time.hours.toString()}" : time.hours.toString(),
                    maxFontSize: 24,
                    minFontSize: 20,
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  ),
            AutoSizeText(
              LocaleKeys.hours.tr(),
              minFontSize: 7,
              maxFontSize: 8,
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
        buildSeparator(),
        Column(
          children: <Widget>[
            time.min == null
                ? AutoSizeText(
                    "00",
                    maxFontSize: 24,
                    minFontSize: 20,
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  )
                : AutoSizeText(
                    time.min! < 10 ? "0${time.min.toString()}" : time.min.toString(),
                    maxFontSize: 24,
                    minFontSize: 20,
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  ),
            AutoSizeText(
              LocaleKeys.minutes.tr(),
              minFontSize: 7,
              maxFontSize: 8,
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
        buildSeparator(),
        Column(
          children: <Widget>[
            time.sec == null
                ? AutoSizeText(
                    "00",
                    maxFontSize: 24,
                    minFontSize: 20,
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  )
                : AutoSizeText(
                    time.sec! < 10 ? "0${time.sec.toString()}" : time.sec.toString(),
                    maxFontSize: 24,
                    minFontSize: 20,
                    style: screenFlag == 0 ? styleDigitHome : styleDigitMarathon,
                  ),
            AutoSizeText(
              LocaleKeys.seconds.tr(),
              minFontSize: 7,
              maxFontSize: 8,
              style: screenFlag == 0 ? styleTextHome : styleTextMarathon,
            ),
          ],
        ),
      ],
    );
  }

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