import 'dart:ui' as ui;

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/main.dart';
import 'package:mohem_flutter_app/ui/marathon/marathon_provider.dart';

class CountdownTimerForMainScreen extends StatelessWidget {
  final int timeToMarathon;
  final MarathonProvider provider;

  CountdownTimerForMainScreen({
    Key? key,
    required this.provider,
    required this.timeToMarathon,
  }) : 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 = TextStyle(
    height: 22 / 27,
    color: MyColors.white,
    fontSize: isTablet ? 30 : 15,
    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 buildSeparator() {
    return AutoSizeText(
      "  :  ",
      maxFontSize: 24,
      minFontSize: 20,
      style: styleDigitHome,
    );
  }

  Widget getTimeDigit(String text) {
    return AutoSizeText(
      text,
      maxFontSize: 24,
      minFontSize: 20,
      style: styleDigitHome,
    );
  }

  Widget getTimeText(String text) {
    return AutoSizeText(
      text,
      minFontSize: 7,
      maxFontSize: 8,
      style: styleTextHome,
    );
  }

  Widget buildEmptyWidget() {
    return Directionality(
      textDirection: ui.TextDirection.ltr,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Column(
            children: <Widget>[
              getTimeDigit("00"),
              getTimeText(LocaleKeys.days.tr()),
            ],
          ),
          buildSeparator(),
          Column(
            children: <Widget>[
              getTimeDigit("00"),
              getTimeText(LocaleKeys.hours.tr()),
            ],
          ),
          buildSeparator(),
          Column(
            children: <Widget>[
              getTimeDigit("00"),
              getTimeText(LocaleKeys.minutes.tr()),
            ],
          ),
          buildSeparator(),
          Column(
            children: <Widget>[
              getTimeDigit("00"),
              getTimeText(LocaleKeys.seconds.tr()),
            ],
          ),
        ],
      ),
    );
  }

  Widget buildCountdownTimer(CurrentRemainingTime? time) {
    if (time == null) {
      return buildEmptyWidget();
    }

    return Directionality(
      textDirection: ui.TextDirection.ltr,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Column(
            children: <Widget>[
              time.days == null ? getTimeDigit("00") : getTimeDigit(time.days! < 10 ? "0${time.days.toString()}" : time.days.toString()),
              getTimeText(LocaleKeys.days.tr()),
            ],
          ),
          buildSeparator(),
          Column(
            children: <Widget>[
              time.hours == null ? getTimeDigit("00") : getTimeDigit(time.hours! < 10 ? "0${time.hours.toString()}" : time.hours.toString()),
              getTimeText(LocaleKeys.hours.tr()),
            ],
          ),
          buildSeparator(),
          Column(
            children: <Widget>[
              time.min == null ? getTimeDigit("00") : getTimeDigit(time.min! < 10 ? "0${time.min.toString()}" : time.min.toString()),
              getTimeText(LocaleKeys.minutes.tr()),
            ],
          ),
          buildSeparator(),
          Column(
            children: <Widget>[
              time.sec == null ? getTimeDigit("00") : getTimeDigit(time.sec! < 10 ? "0${time.sec.toString()}" : time.sec.toString()),
              getTimeText(LocaleKeys.seconds.tr()),
            ],
          ),
        ],
      ),
    );
  }

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