import 'package:app_business/view/home/data_detail.dart';
import 'package:app_business/view/home/event_manage/event_manage_controller.dart';
import 'package:app_business/view/home/personal_rank.dart';
import 'package:app_business/view/home/settings.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:track_common/view.dart';
import 'package:track_common/view/home/field_control/field_control_controller.dart';
import 'package:track_common/view/home/map/map_page.dart';
import 'package:track_common/view/home/personal_rank.dart';

import 'event_manage/event_manage.dart';
import 'field_control.dart';
import 'maplist.dart';

class HomeControllerImpl extends HomeController {
  @override
  int get initTabIdx => 0;

  @override
  List<HomeTab> get tabs => [
        HomeTab(
            '地图',
            () => _TabBuilder<MapPageController>(
                init: () => MapPageControllerImpl(),
                builder: (c) {
                  return const MapPage();
                })),
        HomeTab('场控', () {
          return _TabBuilder<FieldControlController>(
              init: () => FieldControlControllerImpl(),
              builder: (c) {
                return const FieldControlPageImpl();
              });
        }),
        HomeTab(
            '赛事管理',
            () => _TabBuilder<EventManagerController>(
                  init: () => EventManagerController(),
                  builder: (c) {
                    return const EventManage();
                  },
                )),
        HomeTab(
            '排名',
            () => _TabBuilder<PersonalRankController>(
                  init: () => PersonalRankControllerImpl(),
                  builder: (c) {
                    return const PersonalRankPage();
                  },
                )),
        HomeTab(
            '数据详情',
            () => _TabBuilder<DataDetailController>(
                  init: () => DataDetailControllerImpl(),
                  builder: (c) {
                    return const DataDetailPage();
                  },
                )),
        HomeTab('设置', () => const SettingsPage())
      ];
}

class _TabBuilder<T> extends StatefulWidget {
  const _TabBuilder({required this.init, required this.builder});

  final T Function() init;
  final Widget Function(BuildContext contest) builder;

  @override
  State<StatefulWidget> createState() {
    return _TabBuilderState<T>();
  }
}

class _TabBuilderState<T> extends State<_TabBuilder> {
  @override
  void dispose() {
    super.dispose();
    Get.delete<T>();
  }

  @override
  void initState() {
    final c = widget.init();
    tag = '${identityHashCode(c)}';
    Get.put<T>(widget.init());
    super.initState();
    setState(() {
      ok = true;
    });
  }

  late final String tag;
  var ok = false;

  @override
  Widget build(BuildContext context) {
    return ok ? widget.builder(context) : const Center();
  }
}
