import 'personal_rank/personal_rank.dart';
import 'field_control/field_control.dart';
import 'user_manage/user_manage_page.dart';
import 'data_detail/data_detail.dart';
import 'app_bar.dart';
import 'map/map_page.dart';
import 'home_controller.dart';
import 'package:application/widget.dart';

class HomeView extends GetView<HomeController> {
  const HomeView({super.key});

  static Bindings bindings() {
    return BindingsBuilder(() {
      Get.lazyPut<HomeController>(() => HomeController());
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
          appBar: HomeAppBar(
            tab:
                TabBar(
                    controller: controller.tabController,
                    tabs: _tabElems.map((e) => Tab(text: e.title)).toList(),
                    labelColor: Colors.white,
                    unselectedLabelColor: Colors.white,
                    indicatorSize: TabBarIndicatorSize.tab,
                    indicatorPadding: const EdgeInsets.only(bottom: 0),
                    indicator: CustomTabIndicator(),
                    dividerColor: Colors.transparent
                ),
          ),
          body: Container(
            decoration: const BoxDecoration(image: DecorationImage(image: AssetImage(Assets.imagesBkCommonPage), fit: BoxFit.fill)),
            child: TabBarView(
              controller: controller.tabController,
              physics: const NeverScrollableScrollPhysics(),
              children: _tabElems.map((e) => e.child).toList(),
            )
          ) ,
        );
  }
}

final _tabElems = [
  _TabElem(title: '设置', child: const SizedBox()),
  _TabElem(title: '地图', child: const MapPage()),
  _TabElem(title: '场控', child: const FieldControlPage()),
  _TabElem(title: '用户管理', child: const UserManagePage()),
  _TabElem(title: '个人排名', child: PersonalRankPage()),
  _TabElem(title: '分组排名', child: const SizedBox()),
  _TabElem(title: '数据详情', child: const DataDetailPage()),
];

class _TabElem {
  _TabElem({required this.title, required this.child});

  final String title;
  final Widget child;
}
class CustomTabIndicator extends Decoration {
  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return _CustomPainter(this, onChanged!);
  }
}

class _CustomPainter extends BoxPainter {

  final CustomTabIndicator decoration;

  _CustomPainter(this.decoration, VoidCallback onChanged)
      : super(onChanged);

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    final size = configuration.size;
    assert(size != null);
    final Paint paint = Paint();
    paint.color = const Color(0xff003656);
    paint.style = PaintingStyle.fill;
    final path = Path();
    final rect = offset & size!;
    path.moveTo(rect.center.dx - 12 , rect.bottom);
    path .lineTo(rect.center.dx + 12, rect.bottom);
    path.lineTo(rect.center.dx, rect.height - 12);
    path.close();

    canvas.drawPath(path, paint);
  }

}