import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/data/models/response/poultry_science/home_poultry_science/home_poultry_science_model.dart';
import 'package:rasadyar_chicken/presentation/pages/poultry_science/root/logic.dart';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_core/presentation/widget/custom/information_card_widget.dart';

class PoultryScienceHomeLogic extends GetxController {
  PoultryScienceRootLogic rootLogic = Get.find<PoultryScienceRootLogic>();
  Rxn<HomePoultryScienceModel> homeInformation = Rxn();
  RxBool isExpanded = false.obs;
  RxMap<String, List<InformationTagData>> tagInfo = RxMap({
    'first': [
      InformationTagData(
        labelTitle: 'کل فارم ها',
        isLoading: true,
        labelVecIcon: Assets.vec.cubeScanSvg.path,
        iconColor: AppColor.blueFlashing,
        blendMode: BlendMode.srcOut,
        valueBgColor: Colors.white,
        labelGradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: [AppColor.blueLight, Colors.white],
        ),
      ),
      InformationTagData(
        labelTitle: 'تعداد جوجه ریزی',
        isLoading: true,
        labelVecIcon: Assets.vec.cubeCardSvg.path,
        blendMode: BlendMode.dst,
        valueBgColor: Colors.white,
        labelGradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          colors: [AppColor.greenLightHover, Colors.white],
        ),
      ),
    ],
    'second': [
      InformationTagData(
        labelTitle: 'حجم جوجه ریزی',
        unit: 'قطعه',
        isLoading: true,
        labelVecIcon: Assets.vec.hashtagSvg.path,
        iconColor: const Color(0xFF6C5D60),
        labelBgColor: const Color(0xFFDDC0C7),
        valueBgColor: const Color(0xFFEDDCE0),
      ),
      InformationTagData(
        labelTitle: 'مانده در سالن',
        unit: 'قطعه',
        isLoading: true,
        labelVecIcon: Assets.vec.homeHashtagSvg.path,
        labelBgColor: const Color(0xFFAFCBFF),
        valueBgColor: const Color(0xFFCEDFFF),
      ),
    ],
    'third': [
      InformationTagData(
        labelTitle: 'تلفات',
        unit: 'قطعه',
        isLoading: true,
        labelVecIcon: Assets.vec.boxRemoveSvg.path,

        iconColor: const Color(0xFF426060),
        labelBgColor: const Color(0xFFA5D1D2),
        valueBgColor: const Color(0xFFC7DFE0),
      ),
      InformationTagData(
        labelTitle: 'حجم کشتار شده',
        unit: 'قطعه',
        isLoading: true,
        labelVecIcon: Assets.vec.closeSquareFilledSvg.path,
        blendMode: BlendMode.dst,
        labelBgColor: Color(0xFFC8B8D1),
        valueBgColor: Color(0xFFDAD4DD),
      ),
    ],
  });

  RxList<InformationCardData> ageCardData = [
    InformationCardData(
      labelTitle: 'بیشترین سن جوجه ریزی',
      isLoading: true,
      unit: 'روز',
      labelVecIcon: Assets.vec.homeTrendUpSvg.path,
      iconColor: const Color.fromRGBO(85, 97, 93, 1),
      cardBgColor: const Color(0xFFE6FAF5),
      labelBgColor: const Color(0xFFB0EFDF),
    ),
    InformationCardData(
      labelTitle: 'کمترین سن جوجه ریزی',
      isLoading: true,
      unit: 'روز',
      labelVecIcon: Assets.vec.homeTrendDownSvg.path,
      iconColor: const Color(0xFF6F6164),
      cardBgColor: const Color(0xFFEDDCE0),
      labelBgColor: const Color(0xFFE0BCC5),
    ),
  ].obs;

  @override
  void onReady() {
    super.onReady();
    getHomePoultryHatching();
  }

  Future<void> getHomePoultryHatching() async {
    await safeCall<HomePoultryScienceModel?>(
      call: () async => await rootLogic.poultryRepository.getHomePoultry(
        token: rootLogic.tokenService.accessToken.value!,
        type: 'home',
      ),
      onSuccess: (result) {
        if (result != null) {
          homeInformation.value = result;
          tagInfo['first'] = tagInfo['first']!.map((tag) {
            if (tag.labelTitle == 'کل فارم ها') {
              return tag.copyWith(
                value: result.farmCount?.separatedByCommaFa ?? '0',
                isLoading: false,
              );
            }
            if (tag.labelTitle == 'تعداد جوجه ریزی') {
              return tag.copyWith(
                value: result.hatchingCount?.separatedByCommaFa ?? '0',
                isLoading: false,
              );
            }
            return tag;
          }).toList();

          // second
          tagInfo['second'] = tagInfo['second']!.map((tag) {
            switch (tag.labelTitle) {
              case 'حجم جوجه ریزی':
                return tag.copyWith(
                  value: result.hatchingQuantity?.separatedByCommaFa ?? '0',
                  isLoading: false,
                );
              case 'مانده در سالن':
                return tag.copyWith(
                  value: result.hatchingLeftOver?.separatedByCommaFa ?? '0',
                  isLoading: false,
                );
              default:
                return tag;
            }
          }).toList();

          // third
          tagInfo['third'] = tagInfo['third']!.map((tag) {
            switch (tag.labelTitle) {
              case 'تلفات':
                return tag.copyWith(
                  value: result.hatchingLosses?.separatedByCommaFa ?? '0',
                  isLoading: false,
                );
              case 'حجم کشتار شده':
                return tag.copyWith(
                  value: result.hatchingKilledQuantity?.separatedByCommaFa ?? '0',
                  isLoading: false,
                );
              default:
                return tag;
            }
          }).toList();

          ageCardData.value = ageCardData.map((element) {
            switch (element.labelTitle) {
              case 'کمترین سن جوجه ریزی':
                return element.copyWith(
                  value: result.hatchingMinAge?.separatedByCommaFa ?? '0',
                  isLoading: false,
                );
              case 'بیشترین سن جوجه ریزی':
                return element.copyWith(
                  value: result.hatchingMaxAge?.separatedByCommaFa ?? '0',
                  isLoading: false,
                );
              default:
                return element;
            }
          }).toList();
        }
      },
      onError: (error, stackTrace) {},
    );
  }
}
