import 'package:farm_tpf/presentation/screens/plot_detail/sc_plot_history.dart';
import 'package:farm_tpf/presentation/screens/plot_detail/sc_plot_parameter.dart';
import 'package:farm_tpf/utils/const_color.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'sc_plot_action.dart';
import 'sc_plot_information.dart';

class HomeTabbarWidget extends StatefulWidget {
  int? cropId;
  String? cropCode;
  int cropType;
  int initialIndex;
  HomeTabbarWidget({this.cropId, this.initialIndex = 0, this.cropCode, required this.cropType});
  @override
  _HomeTabbarWidgetState createState() => _HomeTabbarWidgetState();
}

class _HomeTabbarWidgetState extends State<HomeTabbarWidget> with TickerProviderStateMixin {
  late TabController tabbarController;
  final homeTabSelected = Get.put(HomeTabbarSelected());
  @override
  void initState() {
    super.initState();
    homeTabSelected.init();
    tabbarController = TabController(vsync: this, length: 4);
    tabbarController.animateTo(widget.initialIndex);
    tabbarController.addListener(() {
      homeTabSelected.changeTab(tabbarController.index);
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.only(top: 4, bottom: 4),
      child: DefaultTabController(
        length: 4,
        child: Scaffold(
          backgroundColor: Colors.white,
          appBar: PreferredSize(
            preferredSize: const Size.fromHeight(40),
            child: Align(
              alignment: Alignment.centerLeft,
              child: GetBuilder<HomeTabbarSelected>(builder: (selectedTab) {
                return TabBar(
                  controller: tabbarController,
                  isScrollable: false,
                  indicatorColor: AppColors.DEFAULT,
                  onTap: (index) {
                    homeTabSelected.changeTab(index);
                  },
                  tabs: [
                    Container(
                      padding: const EdgeInsets.only(top: 8, bottom: 8),
                      child: Text(
                        'Chỉ số',
                        style: TextStyle(color: selectedTab.index == 0 ? AppColors.DEFAULT : Colors.black, fontSize: 12),
                      ),
                    ),
                    Container(
                      padding: const EdgeInsets.only(top: 8, bottom: 8),
                      child: Text('Thông tin', style: TextStyle(color: selectedTab.index == 1 ? AppColors.DEFAULT : Colors.black, fontSize: 12)),
                    ),
                    Container(
                      padding: const EdgeInsets.only(top: 8, bottom: 8),
                      child: Text('Canh tác', style: TextStyle(color: selectedTab.index == 2 ? AppColors.DEFAULT : Colors.black, fontSize: 12)),
                    ),
                    Container(
                      padding: const EdgeInsets.only(top: 8, bottom: 8),
                      child: Text('Lịch sử', style: TextStyle(color: selectedTab.index == 3 ? AppColors.DEFAULT : Colors.black, fontSize: 12)),
                    )
                  ],
                );
              }),
            ),
          ),
          body: TabBarView(
            controller: tabbarController,
            children: [
              PlotParameterScreen(
                cropId: widget.cropId ?? -1,
              ),
              PlotInformationScreen(
                cropId: widget.cropId ?? -1,
              ),
              PlotActionScreen(
                cropId: widget.cropId ?? -1,
                cropCode: widget.cropCode ?? '',
                cropType: widget.cropType,
              ),
              PlotHistoryScreen(
                cropId: widget.cropId ?? -1,
                cropCode: widget.cropCode ?? '',
                cropType: widget.cropType,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class HomeTabbarSelected extends GetxController {
  int index = 0;
  void init() {
    index = 0;
    update();
  }

  void changeTab(int changeIndex) {
    index = changeIndex;
    update();
  }
}
