import 'package:betting/data/model/res/matches/matches.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/statistical_matches/ball_posession_matches/ball_possession_page.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/statistical_matches/cards_matches_regions/cards_matches_page.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/statistical_matches/conversion_rate_matches/conversion_rate_page.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/statistical_matches/estadi_matches/estadi_matches_page.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/statistical_matches/league_position_matches/league_position_page.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/statistical_matches/matches_ended/matches_ended_page.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/statistical_matches/performance_page/performance_page.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/stats_matches_regions/stats_matches_regions_page.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/summary_matches_regions/summary_matches_regions_page.dart';
import 'package:betting/view/mobile/scores/football/detail_tournament/table_tournament/table_tournament_page.dart';
import 'package:chewie/chewie.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:video_player/video_player.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
import 'feed_match/feed_match.dart';
import 'h2h_matches_regions/h2h_matches_regions_page.dart';
import 'infor_matches_regions/infor_matches_regions_page.dart';
import 'line_ups_match/line_up_match_view.dart';

class DetailMatchesRegionsController extends GetxController {
  var listTitle = [
    "Infor",
    "Summary",
    "Stats",
    "Line_Ups",
    "Feed",
    "Table",
    'H2H'
  ];
  var scrollController = ScrollController();
  var itemArgument = Matches().obs;
  var outputTimeFormat = DateFormat('HH:mm');
  var isHeader = true.obs;
  YoutubePlayerController youTubeController = YoutubePlayerController(
      initialVideoId: 'TJD0tYFLNrY',
      flags:
          const YoutubePlayerFlags(autoPlay: false, mute: false, loop: true));
  late VideoPlayerController viedeoController;
  late ChewieController chewiwController;

  @override
  void onInit() {
    var data = Get.arguments;
    if (data != null) {
      itemArgument.value = data;
    }

    print('itemArgument: ${itemArgument.value.toJson()}');

    scrollController.addListener(_onScroll);

    viedeoController = VideoPlayerController.network(
        'https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4');
    chewiwController = ChewieController(
      videoPlayerController: viedeoController,
      allowedScreenSleep: false,
      allowFullScreen: true,
      deviceOrientationsAfterFullScreen: [
        DeviceOrientation.landscapeRight,
        DeviceOrientation.landscapeLeft,
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ],
      autoInitialize: true,
      autoPlay: false,
      showControls: true,
    );
    super.onInit();
  }

  RxInt indexClick = 0.obs;
  var isVisiable = false.obs;
  var isZoom = false.obs;
  var isVisiableYt = false.obs;
  var isVisiablebackgroudYt = false.obs;
  var indexSwiper = 0.obs;
  PageController pageController = PageController(
    initialPage: 0,
    keepPage: true,
  );
  var listPage = <Widget>[
    InforMatchesRegionsPage(),
    SummaryMatchesRegionsPage(),
    StatsMatchesRegionsPage(),
    LineUpMatchPage(),
    FeedMatchView(),
    TableTournamentPage(),
    H2hMatchesRegionsPage()
  ];

  onPageViewChange(int page) {
    indexClick.value = page;
    scrollController.animateTo(0,
        duration: const Duration(microseconds: 1000),
        curve: Curves.bounceInOut);
    if (isHeader.value = false) {
      isHeader.value = false;
    }

    if (page != 0) {
      indexClick.value - 1;
    } else {
      indexClick.value = 0;
    }
  }

  var listPlayer = [
    {'name': "", 'time': ''},
    {'name': "", 'time': ''},
  ];

  var listPlayer2 = [
    {'name': "", 'time': ''},
  ];
  var listStatistical = <Widget>[
    MatchesEndedPage(),
    EstadiMatchesPage(),
    LeaguePositionPage(),
    BallPossessionPage(),
    CoversionRatePage(),
    CardsMatchesPage(),
    PerformancePage(),
  ];

  @override
  void dispose() {
    scrollController.dispose();
    super.dispose();
  }

  void _onScroll() {
    if (scrollController.initialScrollOffset >
            scrollController.position.extentBefore  &&
        !scrollController.position.outOfRange) {
      isHeader.value = false;
    } else {
      isHeader.value = true;
    }
  }

  getMatchTimeText() {
    if (itemArgument.value.status == 'FINISHED') {
      return 'Full time';
    } else if (itemArgument.value.status == 'INIT') {
      DateTime now = DateTime.now();
      String date = itemArgument.value.date!;

      List<int> numbers = date.replaceAll(RegExp(r'\s'), '').split('-').map((e) => int.parse(e)).toList();
      bool isToday = now.year == numbers[0] &&
          now.month == numbers[1] &&
          now.day == numbers[2];

      return isToday == true ? 'Today' : '${itemArgument.value.date}';
    } else if (itemArgument.value.status == 'ADDED TIME') {
      return 'AET';
    } else if (itemArgument.value.status == 'NOT STARTED') {
      return 'AP';
    } else if (itemArgument.value.status == 'IN PLAY') {
      return '${itemArgument.value.time}';
    } else {
      return '';
    }
  }
  checkHeight() {
    if(isVisiablebackgroudYt.value == true){
      return 330.h;
    }else if(isVisiable.value == true){
      return 360.h;
    }else if(isVisiableYt.value == true){
      return  330.h;
    }else{
      return 150.h;
    }
  }
}
