import 'package:agrilink_vocpro/core/extension/extention.dart';
import 'package:agrilink_vocpro/core/route/app_route.dart';
import 'package:agrilink_vocpro/core/state/result_state.dart';
import 'package:agrilink_vocpro/features/home/provider/home_provider.dart';
import 'package:agrilink_vocpro/features/home/widgets/censor_item_loading_widgets.dart';
import 'package:agrilink_vocpro/features/home/widgets/data_display_widget.dart';
import 'package:bootstrap_icons/bootstrap_icons.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

class ListDataFromCensorDht extends StatelessWidget {
  const ListDataFromCensorDht({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Consumer<HomeProvider>(builder: (context, provider, child) {
      switch (provider.dataState) {
        case ResultState.loading:
          return GridView(
            padding: EdgeInsets.all(16.r),
            physics: const NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              crossAxisSpacing: 16.r,
              mainAxisSpacing: 16.r,
              childAspectRatio: 0.9,
            ),
            children: [
              for (int i = 0; i < 4; i++) const CensorItemLoadingWidgets(),
            ],
          );

        case ResultState.hasData:
          return GridView(
            padding: EdgeInsets.all(16.r),
            physics: const NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              crossAxisSpacing: 16.r,
              mainAxisSpacing: 16.r,
              childAspectRatio: 0.9,
            ),
            children: [
              DataDisplayerWidget(
                title: 'Humidity',
                subtitle: 'kelembaban udara',
                value: setTwoDigitAfterComma(provider.dhtHumidity.toDouble()),
                unit: '%',
                icon: BootstrapIcons.droplet_half,
                color: Colors.white,
                censorIdentifier: 'NPK 1',
                onTap: () async {
                  await context
                      .push('${AppRoute.humidity}/${provider.dhtHumidity}');
                },
              ),
              DataDisplayerWidget(
                title: 'Temperature',
                subtitle: 'suhu greenhouse',
                value: provider.dhtTemperature.toString(),
                unit: '°C',
                icon: BootstrapIcons.thermometer_half,
                color: Colors.white,
                onTap: () async {
                  await context.push(
                      '${AppRoute.temperature}/${provider.dhtTemperature}');
                },
              ),
              DataDisplayerWidget(
                title: 'Light',
                subtitle: 'intensitas cahaya',
                value: provider.dhtLuminosity.toString(),
                unit: 'lux',
                icon: BootstrapIcons.sun,
                color: Colors.white,
                onTap: () async {
                  await context
                      .push('${AppRoute.light}/${provider.dhtLuminosity}');
                },
              ),
            ],
          );
        case ResultState.noData:
          return Center(
              child: Image.asset(
            'assets/images/no_data_image.png',
            width: 200.w,
          ));
        case ResultState.error:
          return Center(
              child: Image.asset(
            'assets/images/error_image.png',
            width: 200.w,
          ));
        case ResultState.initial:
          return const SizedBox.shrink();
        default:
          return const SizedBox.shrink();
      }
    });
  }
}
