import 'dart:async';
import 'dart:ui' as ui;

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/config/routes.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:provider/provider.dart';
import 'package:video_player/video_player.dart';

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

  @override
  Widget build(BuildContext context) {
    MarathonProvider provider = context.read<MarathonProvider>();
    return WillPopScope(
      onWillPop: () {
        provider.timerForSponsorVideo.cancel();
        provider.videoController.pause();
        provider.disposeVideoPlayer();
        provider.sponsorsSecondsCounter = 0;
        provider.totalSponsorVideoSeconds = 0;
        return Future<bool>.value(true);
      },
      child: Scaffold(
        backgroundColor: MyColors.black,
        body: SafeArea(
          child: Stack(
            children: <Widget>[
              Align(
                child: provider.videoController.value.isInitialized
                    ? AspectRatio(
                        aspectRatio: provider.videoController.value.aspectRatio,
                        child: VideoPlayer(provider.videoController),
                      )
                    : Container(color: Colors.white),
              ),
              Align(
                alignment: Alignment.topRight,
                child: Container(
                  decoration: BoxDecoration(
                    color: MyColors.white,
                    shape: provider.totalSponsorVideoSeconds == 0 ? BoxShape.circle : BoxShape.rectangle,
                    borderRadius: provider.totalSponsorVideoSeconds == 0 ? null : BorderRadius.circular(15),
                  ),
                  padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 8),
                  child: Consumer<MarathonProvider>(
                    builder: (BuildContext context, MarathonProvider provider, Widget? child) {
                      return provider.totalSponsorVideoSeconds == 0
                          ? InkWell(
                              onTap: () {
                                Navigator.pushReplacementNamed(context, AppRoutes.marathonIntroScreen);
                                provider.videoController.pause();
                                provider.disposeVideoPlayer();
                                provider.sponsorsSecondsCounter = 0;
                                provider.totalSponsorVideoSeconds = 0;
                                provider.timerForSponsorVideo.cancel();
                              },
                              child: const Icon(Icons.close, size: 12),
                            )
                          : Directionality(
                              textDirection: ui.TextDirection.ltr,
                              child: ("${LocaleKeys.ourSponsor.tr()} ${provider.totalSponsorVideoSeconds < 10 ? "0" : ""}${provider.totalSponsorVideoSeconds}").toText12(color: MyColors.darkTextColor),
                            );
                    },
                  ),
                ),
              ).paddingOnly(top: 20, right: 18),
              Consumer<MarathonProvider>(
                builder: (BuildContext context, MarathonProvider provider, Widget? child) {
                  //TODO: WE WILL INCREASE THIS 2 BEFORE GOING LIVE
                  return provider.sponsorsSecondsCounter >= 2
                      ? Align(
                          alignment: Alignment.topLeft,
                          child: InkWell(
                            onTap: () {
                              Navigator.pushReplacementNamed(context, AppRoutes.marathonIntroScreen);
                              provider.videoController.pause();
                              provider.disposeVideoPlayer();
                              provider.sponsorsSecondsCounter = 0;
                              provider.totalSponsorVideoSeconds = 0;
                              provider.timerForSponsorVideo.cancel();
                            },
                            child: Container(
                              decoration: BoxDecoration(color: MyColors.white, borderRadius: BorderRadius.circular(15)),
                              padding: const EdgeInsets.symmetric(vertical: 3, horizontal: 6),
                              child: Directionality(
                                textDirection: ui.TextDirection.ltr,
                                child: LocaleKeys.skip.tr().toText11(color: MyColors.darkTextColor),
                              ),
                            ),
                          ),
                        ).paddingOnly(top: 20, left: 18)
                      : const SizedBox();
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
