import 'package:fis_common/index.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/widgets/webview/webview.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/managers/interfaces/flyinsono_college.dart';
import 'package:flyinsono/managers/interfaces/remote_maintenance.dart';
import 'package:flyinsono/managers/interfaces/webview_controller.dart';
import 'package:flyinsono/managers/server_config_manager.dart';
import 'package:flyinsono/pages/college/controller.dart';
import 'package:flyinsono/views/mobile/past_videos/index.dart';
import 'package:get/get.dart';
import 'package:webviewx/webviewx.dart';
import 'widgets/hover_text_button.dart';
// import 'widgets/media_library.dart';
import 'widgets/mobile_view.dart';
// import 'widgets/my_class/my_class.dart';
import 'widgets/my_course/mock_page.dart';

class FlyinsonoCollege extends FStatefulWidget implements FPage {
  @override
  String get pageName => 'DebugPage';

  @override
  FState<FStatefulWidget> createState() {
    return _FlyinsonoCollegeState();
  }
}

class _FlyinsonoCollegeState extends FState<FlyinsonoCollege> {
  final manager = Get.find<IRemoteMaintenanceManager>();
  final controller = Get.find<FlyinsonoCollegeController>();
  final webviewControllerManager = Get.find<IWebviewControllerManager>();
  final flyinsonoCollegeManager = Get.find<IFlyinsonoCollegeManager>();

  late WebViewXController? webViewController;
  CollegeRouteEnum currentRoute = CollegeRouteEnum.courseHome;
  Uint8List? bytes;
  List<int> cacheBytes = [];

  @override
  FWidget build(BuildContext context) {
    if (kIsMobile) {
      return MobileFlyinsonoCollege();
    }
    return FRow(
      children: [
        _buildLeftNavigation(),
        FVerticalDivider(
          color: Colors.grey,
          width: 1,
          thickness: 1,
        ),
        _buildMainContentView(),
      ],
    );
  }

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

  @override
  void initState() {
    super.initState();
    controller.checkRecordPath();
  }

  FWidget _buildLeftNavigation() {
    return FContainer(
      width: 200,
      color: Color.fromRGBO(233, 248, 255, 1),
      child: FColumn(
        children: [
          _verticalSpace(),
          FContainer(
            margin: EdgeInsets.only(left: 20),
            alignment: Alignment.centerLeft,
            child: FText(
              i18nBook.common.flyinsonoCollege.t,
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
            ),
          ),
          _verticalSpace(),
          FHoverButton(
            text: i18nBook.common.courseHome.t,
            onPressed: () {
              setState(() {
                currentRoute = CollegeRouteEnum.courseHome;
              });
            },
            icon: Icons.home,
            isSelected: currentRoute == CollegeRouteEnum.courseHome,
          ),
          FHoverButton(
            text: i18nBook.college.myCourses.t,
            onPressed: () {
              setState(() {
                currentRoute = CollegeRouteEnum.myCourses;
              });
            },
            icon: Icons.event_note_outlined,
            isSelected: currentRoute == CollegeRouteEnum.myCourses,
          ),
          FHoverButton(
            text: i18nBook.college.pastVideo.t,
            onPressed: () {
              setState(() {
                currentRoute = CollegeRouteEnum.pastVideos;
              });
            },
            icon: Icons.video_library_outlined,
            isSelected: currentRoute == CollegeRouteEnum.pastVideos,
          ),
          // FHoverButton(
          //   text: i18nBook.college.myClasses.t,
          //   onPressed: () {
          //     setState(() {
          //       currentRoute = 2;
          //     });
          //   },
          //   icon: Icons.dvr_outlined,
          //   isSelected: currentRoute == 2,
          // ),
          // FHoverButton(
          //   text: i18nBook.college.myLibrary.t,
          //   icon: Icons.video_library_outlined,
          //   onPressed: () {
          //     setState(() {
          //       currentRoute = 3;
          //     });
          //   },
          //   isSelected: currentRoute == 3,
          // ),
          if (kDebugMode) ...[
            FHoverButton(
              text: 'mock',
              icon: Icons.video_library_outlined,
              onPressed: () {
                setState(() {
                  currentRoute = CollegeRouteEnum.mock;
                });
              },
              isSelected: currentRoute == CollegeRouteEnum.mock,
            ),
          ],
          FExpanded(child: FSizedBox()),
          if (FPlatform.isMacOS || FPlatform.isWindows || kDebugMode)
            FHoverButton(
              text: i18nBook.college.recordSettings.t,
              onPressed: () {
                controller.openRecordSettingDialog();
              },
              icon: Icons.settings,
              isSelected: false,
              clickOnly: true,
              fontSize: i18nBook.isCurrentChinese ? 18 : 13,
            ),
          FSizedBox(
            height: 10,
          ),
        ],
      ),
    );
  }

  FWidget _verticalSpace() {
    return const FSizedBox(height: 15);
  }

  FWidget _buildMainContentView() {
    switch (currentRoute) {
      case CollegeRouteEnum.courseHome:
        return FExpanded(
          child: FLayoutBuilder(
            key: UniqueKey(),
            builder: (context, constraints) {
              final webview = FWebViewX(
                height: constraints.maxHeight,
                width: constraints.maxWidth,
                // initialContent: "http://localhost:5173/#/" +
                //     'all_courses/${Store.user.token}/${FPlatform.current.index}/${i18nBook.locale.languageCode.toLowerCase()}',
                initialContent: ServerConfigManager.flyinsonoSchoolUrl +
                    'all_courses/${Store.user.token}/${FPlatform.current.index}/${i18nBook.locale.languageCode.toLowerCase()}',
                initialSourceType: SourceType.url,
                javascriptMode: JavascriptMode.unrestricted,
                ignoreAllGestures:
                    kIsMobile ? false : webviewControllerManager.isDialogExist,
                onWebViewCreated: (controller) {
                  webViewController = controller;
                  this.controller.kActiveWebViewController = controller;
                  webviewControllerManager.initWebviewController(controller);
                },
              );
              return FSizedBox.expand(
                // height: MediaQuery.of(context).size.height,
                // width: MediaQuery.of(context).size.width,
                child: webview,
              );
            },
          ),
        );
      case CollegeRouteEnum.myCourses:
        // var url = "http://localhost:5173/#/" +
        //     'my_course/${Store.user.token}/${FPlatform.current.index}/${i18nBook.locale.languageCode.toLowerCase()}';
        var url = ServerConfigManager.flyinsonoSchoolUrl +
            'my_course/${Store.user.token}/${3}/${i18nBook.locale.languageCode.toLowerCase()}';
        return FExpanded(
          child: FLayoutBuilder(
            key: UniqueKey(),
            builder: (context, constraints) {
              final webview = FWebViewX(
                height: constraints.maxHeight,
                width: constraints.maxWidth,
                initialContent: url,
                initialSourceType: SourceType.url,
                javascriptMode: JavascriptMode.unrestricted,
                ignoreAllGestures:
                    kIsMobile ? false : webviewControllerManager.isDialogExist,
                onWebViewCreated: (controller) {
                  webViewController = controller;
                  this.controller.kActiveWebViewController = controller;
                  webviewControllerManager.initWebviewController(controller);
                },
              );
              return FSizedBox.expand(
                // height: MediaQuery.of(context).size.height,
                // width: MediaQuery.of(context).size.width,
                child: webview,
              );
            },
          ),
        );
      case CollegeRouteEnum.pastVideos:
        return FExpanded(
          child: FClipRRect(
            child: FisBuilder(
              init: PastVideosController(),
              builder: (_) {
                return PastVideosPage();
              },
            ),
          ),
        );
      // case 2:
      //   return MyClass();
      // case 3:
      //   controller.getMediaLibrarys();
      //   return MediaLibrary();
      case CollegeRouteEnum.mock:
        return MockPage();
      default:
        return FSizedBox();
    }
  }
}

enum CollegeRouteEnum {
  courseHome,
  myCourses,
  pastVideos,
  mock,
}
