import 'package:track_common/widget/prelude.dart';

import 'app_bar.dart';
import 'home_controller.dart';

class HomeView extends GetView<HomeController> {
  const HomeView({super.key});
  static const name = '/HomeView';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HomeAppBar(
        tab: TabBar(
            controller: controller.tabController,
            tabs: controller.tabs.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, package: package),
                  fit: BoxFit.fill)),
          child: TabBarView(
            controller: controller.tabController,
            physics: const NeverScrollableScrollPhysics(),
            children: controller.tabs.map((e) => e.builder()).toList(),
          )),
    );
  }
}

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);
  }
}
