import 'package:flutter/material.dart';
import 'package:syncrow_app/features/devices/view/widgets/power_clamp/power_info_card.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
import 'power_chart.dart';

class PowerClampCard extends StatelessWidget {
  final bool? isGeneral;
  final String? title;
  final String? totalCurrent;
  final String? totalActiveGeneral;
  final String? totalCurrentGeneral;
  final String? totalFrequencyGeneral;
  final String? totalVoltage;
  final String? totalActive;
  final String? totalFrequency;
  final String? dateTimeSelected;
  final String? totalFactor;
  final Widget? dateSwitcher;
  final String? formattedDate;
  final String? phaseType;

  final String? energyConsumption;
  final Function()? selectDateEvent;
  final List<EnergyData>? chartData;
  final BuildContext? context;

  const PowerClampCard({
    Key? key,
    this.isGeneral,
    this.title,
    this.totalCurrent,
    this.totalActiveGeneral,
    this.dateTimeSelected,
    this.totalCurrentGeneral,
    this.totalFrequencyGeneral,
    this.totalVoltage,
    this.phaseType,
    this.totalActive,
    this.totalFrequency,
    this.totalFactor,
    this.dateSwitcher,
    this.formattedDate,
    this.selectDateEvent,
    this.chartData,
    this.context,
    this.energyConsumption,
    //nConsumption
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DefaultContainer(
      child: Padding(
        padding: const EdgeInsets.only(left: 5, right: 5, top: 10, bottom: 10),
        child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text('Energy usage'),
              Row(
                crossAxisAlignment: CrossAxisAlignment.end,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  BodyLarge(
                    text: title!,
                    fontSize: 20,
                    fontWeight: FontWeight.w700,
                  ),
                  Row(
                    children: [
                      BodyLarge(
                        text: energyConsumption!,
                        fontSize: 20,
                        fontWeight: FontWeight.w700,
                      ),
                      const BodySmall(text: 'kWh')
                    ],
                  ),
                ],
              ),
              const SizedBox(
                height: 10,
              ),
              isGeneral == true
                  ? Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        PowerClampInfoCard(
                          iconPath: Assets.powerActiveIcon,
                          title: 'Active',
                          value: '$totalActiveGeneral',
                          unit: ' w',
                        ),
                        PowerClampInfoCard(
                          iconPath: Assets.voltMeterIcon,
                          title: 'Current',
                          value: '$totalCurrentGeneral',
                          unit: ' A',
                        ),
                        PowerClampInfoCard(
                          iconPath: Assets.frequencyIcon,
                          title: 'Frequency',
                          value: '$totalFrequencyGeneral',
                          unit: ' Hz',
                        ),
                      ],
                    )
                  : Column(
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            PowerClampInfoCard(
                              iconPath: Assets.voltageIcon,
                              title: 'Voltage',
                              value: '${double.parse(totalVoltage!) / 10}',
                              unit: ' V',
                            ),
                            PowerClampInfoCard(
                              iconPath: Assets.voltMeterIcon,
                              title: 'Current',
                              value: '$totalCurrent',
                              unit: ' A',
                            ),
                          ],
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            PowerClampInfoCard(
                              iconPath: Assets.powerActiveIcon,
                              title: 'Active Power',
                              value: '$totalActive',
                              unit: ' w',
                            ),
                            PowerClampInfoCard(
                              iconPath: Assets.speedoMeter,
                              title: 'Power Factor',
                              value: '$totalFactor',
                              unit: '',
                            ),
                          ],
                        )
                      ],
                    ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      BodyMedium(
                        text: isGeneral == true
                            ? 'Total consumption'
                            : phaseType!,
                        fontSize: 12,
                        fontWeight: FontWeight.w700,
                      ),
                      Text(
                        dateTimeSelected!,
                        style: const TextStyle(
                            fontSize: 8, fontWeight: FontWeight.w400),
                      ),
                    ],
                  ),
                  const Row(
                    children: [
                      BodyMedium(
                          text: '1000.00 ',
                          fontSize: 12,
                          fontWeight: FontWeight.w700),
                      BodyMedium(
                          text: 'kWh',
                          fontSize: 8,
                          fontWeight: FontWeight.w700),
                    ],
                  ),
                ],
              ),
              Expanded(
                child: SizedBox(
                  child: EnergyConsumptionPage(
                    chartData: chartData!.isNotEmpty
                        ? chartData!
                        : [
                            EnergyData('12:00 AM', 4.0),
                            EnergyData('01:00 AM', 3.5),
                            EnergyData('02:00 AM', 3.8),
                            EnergyData('03:00 AM', 3.2),
                            EnergyData('04:00 AM', 4.0),
                            EnergyData('05:00 AM', 3.4),
                            EnergyData('06:00 AM', 3.2),
                            EnergyData('07:00 AM', 3.5),
                            EnergyData('08:00 AM', 3.8),
                            EnergyData('09:00 AM', 3.6),
                            EnergyData('10:00 AM', 3.9),
                            EnergyData('11:00 AM', 4.0),
                          ],
                    totalConsumption: chartData!
                        .fold(0, (sum, data) => sum + data.consumption),
                    date: '10/08/2024',
                  ),
                ),
              ),
              const SizedBox(
                height: 5,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  DefaultContainer(
                      padding: EdgeInsets.all(0),
                      color: ColorsManager.grayBox,
                      child: SizedBox(
                        child: dateSwitcher,
                      )),
                  InkWell(
                    onTap: selectDateEvent,
                    child: DefaultContainer(
                        color: ColorsManager.grayBox,
                        child: SizedBox(
                          child: Padding(
                            padding: const EdgeInsets.all(5),
                            child: Text(formattedDate!),
                          ),
                        )),
                  ),
                ],
              )
            ]),
      ),
    );
  }
}
