import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:go_router/go_router.dart';
import 'package:graphview/GraphView.dart';
import 'package:syncrow_web/pages/auth/model/user_model.dart';
import 'package:syncrow_web/pages/home/bloc/home_event.dart';
import 'package:syncrow_web/pages/home/bloc/home_state.dart';
import 'package:syncrow_web/pages/home/home_model/home_item_model.dart';
import 'package:syncrow_web/services/home_api.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/constants/routes_const.dart';

class HomeBloc extends Bloc<HomeEvent, HomeState> {
  final Graph graph = Graph()..isTree = true;
  final BuchheimWalkerConfiguration builder = BuchheimWalkerConfiguration();
  List<Node> sourcesList = [];
  List<Node> destinationsList = [];
  UserModel? user;

  HomeBloc() : super((HomeInitial())) {
    on<CreateNewNode>(_createNode);
    on<FetchUserInfo>(_fetchUserInfo);
  }

  void _createNode(CreateNewNode event, Emitter<HomeState> emit) async {
    emit(HomeInitial());
    sourcesList.add(event.sourceNode);
    destinationsList.add(event.destinationNode);
    for (int i = 0; i < sourcesList.length; i++) {
      graph.addEdge(sourcesList[i], destinationsList[i]);
    }

    builder
      ..siblingSeparation = (100)
      ..levelSeparation = (150)
      ..subtreeSeparation = (150)
      ..orientation = (BuchheimWalkerConfiguration.ORIENTATION_TOP_BOTTOM);
    emit(HomeUpdateTree(graph: graph, builder: builder));
  }

  Future _fetchUserInfo(FetchUserInfo event, Emitter<HomeState> emit) async {
    try {
      var uuid =
          await const FlutterSecureStorage().read(key: UserModel.userUuidKey);
      user = await HomeApi().fetchUserInfo(uuid);
      emit(HomeInitial());
    } catch (e) {
      return;
    }
  }

//  static  Future fetchUserInfo() async {
//     try {
//       var uuid =
//           await const FlutterSecureStorage().read(key: UserModel.userUuidKey);
//       user = await HomeApi().fetchUserInfo(uuid);
//     } catch (e) {
//       return;
//     }
//   }

  List<HomeItemModel> homeItems = [
    HomeItemModel(
      title: 'Access',
      icon: Assets.accessIcon,
      active: true,
      onPress: (context) {
        context.go(RoutesConst.accessManagementPage);
      },
      color: null,
    ),
    HomeItemModel(
      title: 'Space Management',
      icon: Assets.spaseManagementIcon,
      active: true,
      onPress: (context) {},
      color: ColorsManager.primaryColor,
    ),
    HomeItemModel(
      title: 'Devices',
      icon: Assets.devicesIcon,
      active: true,
      onPress: (context) {
        context.go(RoutesConst.deviceManagementPage);
      },
      color: ColorsManager.primaryColor,
    ),
    HomeItemModel(
      title: 'Move in',
      icon: Assets.moveinIcon,
      active: false,
      onPress: (context) {},
      color: ColorsManager.primaryColor,
    ),
    HomeItemModel(
      title: 'Construction',
      icon: Assets.constructionIcon,
      active: false,
      onPress: (context) {},
      color: ColorsManager.primaryColor,
    ),
    HomeItemModel(
      title: 'Energy',
      icon: Assets.energyIcon,
      active: false,
      onPress: (context) {},
      color: ColorsManager.slidingBlueColor.withOpacity(0.2),
    ),
    HomeItemModel(
      title: 'Integrations',
      icon: Assets.integrationsIcon,
      active: false,
      onPress: (context) {},
      color: ColorsManager.slidingBlueColor.withOpacity(0.2),
    ),
    HomeItemModel(
      title: 'Asset',
      icon: Assets.assetIcon,
      active: false,
      onPress: (context) {},
      color: ColorsManager.slidingBlueColor.withOpacity(0.2),
    ),
  ];
}
