import 'package:agrilink_vocpro/core/constant/app_color.dart';
import 'package:agrilink_vocpro/core/constant/app_theme.dart';
import 'package:agrilink_vocpro/core/extension/extention.dart';
import 'package:agrilink_vocpro/features/home/provider/home_provider.dart';
import 'package:agrilink_vocpro/features/home/widgets/list_data_from_censor_npk1.dart';
import 'package:agrilink_vocpro/features/home/widgets/list_data_from_censor_npk2.dart';
import 'package:agrilink_vocpro/features/home/widgets/list_data_from_censor_dht.dart';
import 'package:agrilink_vocpro/features/setting/provider/setting_provider.dart';
import 'package:animated_segmented_tab_control/animated_segmented_tab_control.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          toolbarHeight: 200.h,
          elevation: 0,
          backgroundColor: Colors.white,
          scrolledUnderElevation: 0,
          flexibleSpace: Padding(
            padding: EdgeInsets.symmetric(horizontal: 12.w),
            child:
                Consumer<SettingProvider>(builder: (context, settingP, child) {
              return Column(
                children: [
                  Row(
                    children: [
                      CircleAvatar(
                        radius: 24.r,
                        backgroundColor: AppColor.primary,
                        child: Icon(
                          Icons.person,
                          color: Colors.white,
                          size: 24.r,
                        ),
                      ),
                      SizedBox(width: 16.w),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            getGreeting(DateTime.now().toString()),
                            style: AppTheme.labelSmall,
                          ),
                          Text(
                            settingP.userFullName,
                            style: AppTheme.labelMedium,
                          ),
                        ],
                      ),
                      const Spacer(),
                      IconButton(
                        onPressed: () {
                          context.read<HomeProvider>().getLatestData();
                        },
                        icon: const Icon(
                          Icons.refresh_rounded,
                          color: AppColor.primary,
                        ),
                      )
                    ],
                  ),
                  SizedBox(
                    height: 8.h,
                  ),
                  Container(
                    padding: const EdgeInsets.all(16),
                    height: MediaQuery.of(context).size.height * 0.17,
                    decoration: BoxDecoration(
                      color: AppColor.secondary,
                      image: const DecorationImage(
                          image:
                              AssetImage('assets/images/green_house_image.jpg'),
                          fit: BoxFit.cover),
                      borderRadius: BorderRadius.circular(16),
                    ),
                    child: Column(
                      children: [
                        Container(
                          decoration: BoxDecoration(
                            color: AppColor.ternary.withAlpha(200),
                            borderRadius: BorderRadius.circular(32),
                          ),
                          padding: const EdgeInsets.symmetric(
                              vertical: 8, horizontal: 16),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("${settingP.userFullName}'s",
                                  style: AppTheme.labelMedium
                                      .copyWith(color: Colors.white)),
                              Container(
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 12, vertical: 8),
                                decoration: BoxDecoration(
                                  color: AppColor.primary,
                                  borderRadius: BorderRadius.circular(16),
                                ),
                                child: Text(
                                  dateFormater(DateTime.now().toString()),
                                  style: AppTheme.labelMedium
                                      .copyWith(color: Colors.white),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              );
            }),
          ),
        ),
        body: DefaultTabController(
            length: 3,
            child: Stack(
              children: [
                Padding(
                  padding:
                      EdgeInsets.symmetric(horizontal: 64.w, vertical: 16.h),
                  child: SegmentedTabControl(
                      height: 32.h,
                      tabTextColor: Colors.black,
                      tabPadding: EdgeInsets.zero,
                      barDecoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(24.r),
                      ),
                      indicatorDecoration: BoxDecoration(
                        color: AppColor.primary,
                        borderRadius: BorderRadius.circular(24.r),
                      ),
                      textStyle: AppTheme.labelSmall,
                      tabs: const [
                        SegmentTab(label: 'DHT'),
                        SegmentTab(label: 'NPK 1'),
                        SegmentTab(label: 'NPK 2'),
                      ]),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 64.h),
                  child: const TabBarView(children: [
                    ListDataFromCensorDht(),
                    ListDataFromCensorNpk1(),
                    ListDataFromCensorNpk2(),
                  ]),
                )
              ],
            )),
      ),
    );
  }
}
