import 'dart:ui';

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/menu/bloc/privacy_policy.dart';
import 'package:syncrow_app/features/menu/bloc/user_agreement.dart';
import 'package:syncrow_app/features/menu/view/widgets/join_home/join_home_view.dart';
import 'package:syncrow_app/features/menu/view/widgets/manage_home/manage_home_view.dart';
import 'package:syncrow_app/features/menu/view/widgets/securty/view/securty_view.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/services/api/profile_api.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

part 'menu_state.dart';

class MenuCubit extends Cubit<MenuState> {
  MenuCubit() : super(MenuInitial());
  static MenuCubit of(context) => BlocProvider.of<MenuCubit>(context);

  String name = '';
  String userAgreementHtml = "";
  String privacyPolicyHtml = "";

  Future<void> fetchAgreement() async {
    try {
      emit(MenuLoading());
      final response = await ProfileApi().fetchUserAgreement();
      userAgreementHtml = response;
      emit(MenuLoaded(userAgreementHtml));
    } catch (error) {
      emit(MenuError(error.toString()));
    }
  }

  Future<void> fetchPrivacyPolicy() async {
    try {
      emit(MenuLoading());
      final response = await ProfileApi().fetchPrivacyPolicy();
      privacyPolicyHtml = response;
      emit(MenuLoaded(privacyPolicyHtml));
    } catch (error) {
      emit(MenuError(error.toString()));
    }
  }

  List<Map<String, Object>> menuSections = [
    //Home Management
    {
      'title': 'Home Management',
      'color': ColorsManager.primaryColor,
      'buttons': [
        // {
        //   'title': 'Create a Unit',
        //   'Icon': Assets.assetsIconsMenuIconsHomeManagementIconsCreateHome,
        //   'page': const CreateUnitView()
        // },
        {
          'title': 'Join a Unit',
          'Icon': Assets.assetsIconsMenuIconsHomeManagementIconsJoinAHome,
          'page': const JoinHomeView()
        },
        if (HomeCubit.manageSupSpace)
          {
            'title': 'Manage Your Units',
            'Icon':
                Assets.assetsIconsMenuIconsHomeManagementIconsManageYourHome,
            'page': const ManageHomeView()
          },
      ],
    },
    //General Settings
    // {
    //   'title': 'General Settings',
    //   'color': const Color(0xFF023DFE),
    //   'buttons': [
    //     {
    //       'title': 'Voice Assistant',
    //       'Icon': Assets.assetsIconsMenuIconsGeneralSettingsIconsVoiceAssistant,
    //       'page': null
    //     },
    //     {
    //       'title': 'Temperature unit',
    //       'Icon': Assets.assetsIconsMenuIconsGeneralSettingsIconsTemperatureUnit,
    //       'page': null
    //     },
    //     {
    //       'title': 'Touch tone on panel',
    //       'Icon': Assets.assetsIconsMenuIconsGeneralSettingsIconsTouchTone,
    //       'page': null
    //     },
    //     {
    //       'title': 'Language',
    //       'Icon': Assets.assetsIconsMenuIconsGeneralSettingsIconsLanguage,
    //       'page': null
    //     },
    //     {
    //       'title': 'Network Diagnosis',
    //       'Icon': Assets.assetsIconsMenuIconsGeneralSettingsIconsNetworkDiagnosis,
    //       'page': null
    //     },
    //     {
    //       'title': 'Clear Cache',
    //       'Icon': Assets.assetsIconsMenuIconsGeneralSettingsIconsClearCach,
    //       'page': null
    //     },
    //   ],
    // },
    // //Messages Center
    // {
    //   'title': 'Messages Center',
    //   'color': const Color(0xFF0088FF),
    //   'buttons': [
    //     {
    //       'title': 'Alerts',
    //       'Icon': Assets.assetsIconsMenuIconsMessagesCenterIconsAlerts,
    //       'page': null
    //     },
    //     {
    //       'title': 'Messages',
    //       'Icon': Assets.assetsIconsMenuIconsMessagesCenterIconsMessages,
    //       'page': null
    //     },
    //     {
    //       'title': 'FAQs',
    //       'Icon': Assets.assetsIconsMenuIconsMessagesCenterIconsFAQs,
    //       'page': null
    //     },
    //     {
    //       'title': 'Help & Feedback',
    //       'Icon': Assets.assetsIconsMenuIconsMessagesCenterIconsHelpAndFeedback,
    //       'page': null
    //     },
    //   ],
    // },
    //Security And Privacy
    {
      'title': 'Security And Privacy',
      'color': const Color(0xFF8AB9FF),
      'buttons': [
        {
          'title': 'Security',
          'Icon': Assets.assetsIconsMenuIconsSecurityAndPrivacyIconsSecurty,
          'page': const SecurtyView()
        },
        // {
        //   'title': 'Privacy',
        //   'Icon': Assets.assetsIconsMenuIconsSecurityAndPrivacyIconsPrivacy,
        //   'page': const PrivacyView()
        // },
      ],
    },
    //Legal Information
    {
      'title': 'Legal Information',
      'color': const Color(0xFF001B72),
      'buttons': [
        // {
        //   'title': 'About',
        //   'Icon': Assets.assetsIconsMenuIconsLeagalInfoIconsAbout,
        //   'page': null
        // },
        {
          'title': 'Privacy Policy',
          'Icon': Assets.assetsIconsMenuIconsLeagalInfoIconsPrivacyPolicy,
          'page': const PrivacyPolicy()
        },
        {
          'title': 'User Agreement',
          'Icon': Assets.assetsIconsMenuIconsLeagalInfoIconsUserAgreement,
          'page': const UserAgreement()
        },
      ],
    },
  ];

  Future<void> fetchMenuSections() async {
    emit(MenuLoading());
    try {
      emit(MenuItemsLoaded(menuSections));
    } catch (e) {
      emit(MenuError(e.toString()));
    }
  }
}
