import 'package:agrilink_vocpro/core/constant/app_theme.dart';
import 'package:agrilink_vocpro/core/state/result_state.dart';
import 'package:agrilink_vocpro/features/home/detail_pages/nitrogen/provider/nitrogen_provider.dart';
import 'package:agrilink_vocpro/features/home/widgets/graphic_error_widget.dart';
import 'package:agrilink_vocpro/features/home/widgets/graphic_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:provider/provider.dart';

class NitrogenScreen extends StatelessWidget {
  const NitrogenScreen(
      {super.key, this.nitrogenNpk1 = 0.0, this.nitrogenNpk2 = 0.0});

  final double nitrogenNpk1;
  final double nitrogenNpk2;

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => NitrogenProvider(),
      child: DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            title: Text('Soil Nitrogen', style: AppTheme.labelMedium),
            centerTitle: true,
            backgroundColor: Colors.white,
            scrolledUnderElevation: 0,
            actions: const [
              Padding(
                padding: EdgeInsets.only(right: 16),
                child: Icon(
                  BootstrapIcons.eyedropper,
                  color: Colors.blue,
                ),
              )
            ],
            bottom: const TabBar(
              tabs: [
                Tab(text: 'NPK 1'),
                Tab(text: 'NPK 2'),
              ],
            ),
          ),
          body: TabBarView(
            children: [
              buildTabContent(context, nitrogenNpk1, 'NPK 1', true),
              buildTabContent(context, nitrogenNpk2, 'NPK 2', false),
            ],
          ),
        ),
      ),
    );
  }

  SafeArea buildTabContent(
      BuildContext context, double value, String label, bool isNpk1) {
    return SafeArea(
      child: ListView(
        padding: EdgeInsets.all(16.w),
        children: [
          SizedBox(height: MediaQuery.of(context).size.height * 0.05),
          buildSoilInfo(context, value),
          SizedBox(height: 16.h),
          buildInfoRow(context),
          SizedBox(height: 16.h),
          const Text('Grafik'),
          SizedBox(height: 16.h),
          buildGraphicContent(context, isNpk1),
        ],
      ),
    );
  }

  Widget buildSoilInfo(BuildContext context, double value) {
    return Center(
      child: Column(
        children: [
          Icon(
            CupertinoIcons.eyedropper,
            size: 64.r,
            color: Colors.blue,
          ),
          Text('$value ppm', style: AppTheme.headline1),
        ],
      ),
    );
  }

  Widget buildInfoRow(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          'Soil Nitrogen',
          style: AppTheme.labelMedium,
          textAlign: TextAlign.center,
        ),
        IconButton(
          iconSize: 20.r,
          color: Colors.blue,
          onPressed: () {},
          icon: const Icon(BootstrapIcons.info_circle),
        ),
      ],
    );
  }

  Widget buildGraphicContent(BuildContext context, bool isNpk1) {
    return AspectRatio(
      aspectRatio: 1.6.h,
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(16.w),
          border: Border.all(color: Colors.grey.shade300, width: 1.w),
        ),
        child: Consumer<NitrogenProvider>(
          builder: (context, provider, child) {
            final dataState = provider.dataState;

            switch (dataState) {
              case ResultState.loading:
                return const Center(child: CupertinoActivityIndicator());
              case ResultState.hasData:
                return GarphicWidget(
                  gradientColors: const [Colors.blue, Colors.blueAccent],
                  hour: List.generate(
                    isNpk1
                        ? provider.dataFetchedNpk1.length
                        : provider.dataFetchedNpk2.length,
                    (index) => isNpk1
                        ? provider.dataFetchedNpk1[index].hour ?? 0
                        : provider.dataFetchedNpk2[index].hour ?? 0,
                  ),
                  data: List.generate(
                    isNpk1
                        ? provider.dataFetchedNpk1.length
                        : provider.dataFetchedNpk2.length,
                    (index) => isNpk1
                        ? provider.dataFetchedNpk1[index].soilnitrogen ?? 0
                        : provider.dataFetchedNpk2[index].soilnitrogen ?? 0,
                  ),
                  maxValue: 1,
                );
              case ResultState.error:
                return const GraphicErrorWidget(message: 'Terjadi Kesalahan');
              case ResultState.noData:
                return const GraphicErrorWidget(message: 'Tidak Ada Data');
              case ResultState.initial:
              default:
                return const SizedBox.shrink();
            }
          },
        ),
      ),
    );
  }
}
