import 'package:agrilink_vocpro/core/constant/app_color.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/cupertino.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 ListDataFromCensorNpk1 extends StatelessWidget {
  const ListDataFromCensorNpk1({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    const String censorIdentifier = 'NPK 1';
    return Consumer<HomeProvider>(builder: (context, provider, child) {
      switch (provider.dataState) {
        case ResultState.loading:
          return GridView(
            padding: EdgeInsets.all(16.r),
            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),
            shrinkWrap: true,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              crossAxisSpacing: 16.r,
              mainAxisSpacing: 16.r,
              childAspectRatio: 0.9,
            ),
            children: [
              DataDisplayerWidget(
                title: 'Nitrogen',
                subtitle: 'Kadar Nitrogen',
                value: provider.npk1SoilNitrogen.toString(),
                unit: 'mg/L',
                icon: CupertinoIcons.eyedropper,
                textColor: Colors.white,
                color: AppColor.secondary,
                iconColor: Colors.white,
                onTap: () async {
                  await context.push(
                      '${AppRoute.nitrogen}/${provider.npk2SoilNitrogen}/${provider.npk1SoilNitrogen}');
                },
              ),
              DataDisplayerWidget(
                title: 'Phosphorus',
                subtitle: 'Kadar Fosfor',
                value: provider.npk1SoilPhosphorus.toString(),
                unit: 'mg/L',
                icon: CupertinoIcons.eyedropper,
                textColor: Colors.white,
                color: AppColor.secondary,
                iconColor: Colors.white,
                onTap: () async {
                  await context.push(
                      '${AppRoute.phosphorus}/${provider.npk1SoilPhosphorus}/${provider.npk2SoilPhosphorus}');
                },
              ),
              DataDisplayerWidget(
                title: 'Kalium',
                subtitle: 'Kadar kalium',
                value: provider.npk1SoilPotassium.toString(),
                unit: 'mg/L',
                icon: CupertinoIcons.eyedropper,
                textColor: Colors.white,
                color: AppColor.secondary,
                iconColor: Colors.white,
                onTap: () async {
                  await context.push(
                      '${AppRoute.potassium}/${provider.npk1SoilPotassium}/${provider.npk2SoilPotassium}');
                },
              ),
              DataDisplayerWidget(
                title: 'Temperature',
                subtitle: 'Suhu tanah',
                value: provider.npk1Temperature.toString(),
                unit: '°C',
                icon: BootstrapIcons.thermometer_half,
                color: Colors.white,
                censorIdentifier: censorIdentifier,
                onTap: () async {
                  await context.push(
                      '${AppRoute.soilTemperature}/${provider.npk1Temperature}/${provider.npk2Temperature}');
                },
              ),
              DataDisplayerWidget(
                title: 'Soil Moisture',
                subtitle: 'kelembaban tanah',
                value: provider.npk1SoilMoisture.toString(),
                unit: '%',
                icon: Icons.water_outlined,
                color: Colors.white,
                censorIdentifier: censorIdentifier,
                onTap: () async {
                  await context.push(
                      '${AppRoute.soilMoisture}/${provider.npk1SoilMoisture}/${provider.npk2SoilMoisture}');
                },
              ),
              DataDisplayerWidget(
                title: 'Acid Level (PH)',
                subtitle: 'tingkat keasaman',
                value: provider.npk1SoilPh.toString(),
                unit: 'pH',
                icon: BootstrapIcons.pie_chart,
                color: Colors.white,
                censorIdentifier: censorIdentifier,
                onTap: () async {
                  context.push(
                      '${AppRoute.ph}/${provider.npk1SoilPh}/${provider.npk2SoilPh}');
                },
              ),
              DataDisplayerWidget(
                title: 'Conductivity',
                subtitle: 'Daya Arus Listrik',
                value: provider.npk1SoilEc.toString(),
                unit: 'µS/cm',
                icon: Icons.electric_bolt,
                color: Colors.white,
                censorIdentifier: censorIdentifier,
                onTap: () async {
                  await context.push(
                      '${AppRoute.conductivity}/${provider.npk1SoilEc}/${provider.npk2SoilEc}');
                },
              ),
            ],
          );
        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,
          ));
        default:
          return const SizedBox.shrink();
      }
    });
  }
}
