import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'inspection_topology_state.dart';

class InspectionTopologyCubit extends Cubit<InspectionTopologyState> {
  InspectionTopologyCubit()
      : super(const InspectionTopologyState(
          deviceName: '',
          deviceModel: '',
          topologyNodes: [],
        )) {
    _loadTopologyData();
  }

  void selectNode(TopologyNode? node) {
    emit(state.copyWith(selectedNode: node));
  }

  void _loadTopologyData() {
    final rootNode = TopologyNode(
      id: '1',
      name: '智能空开',
      status: 'online',
      x: 0.5,
      y: 0.15,
      children: [
        TopologyNode(
          id: '2',
          name: '串口分配器',
          status: 'online',
          x: 0.25,
          y: 0.45,
          children: [
            TopologyNode(
              id: '5',
              name: '环境采集',
              status: 'online',
              x: 0.15,
              y: 0.75,
              children: [],
            ),
            TopologyNode(
              id: '6',
              name: '人体存在',
              status: 'online',
              x: 0.35,
              y: 0.75,
              children: [],
            ),
          ],
        ),
        TopologyNode(
          id: '3',
          name: '网关',
          status: 'online',
          x: 0.5,
          y: 0.45,
          children: [
            TopologyNode(
              id: '7',
              name: '环境采集',
              status: 'online',
              x: 0.45,
              y: 0.75,
              children: [],
            ),
            TopologyNode(
              id: '8',
              name: '人体存在',
              status: 'online',
              x: 0.55,
              y: 0.75,
              children: [],
            ),
          ],
        ),
        TopologyNode(
          id: '4',
          name: 'WIFI',
          status: 'online',
          x: 0.75,
          y: 0.45,
          children: [
            TopologyNode(
              id: '9',
              name: '环境采集',
              status: 'online',
              x: 0.65,
              y: 0.75,
              children: [],
            ),
            TopologyNode(
              id: '10',
              name: '人体存在',
              status: 'online',
              x: 0.85,
              y: 0.75,
              children: [],
            ),
          ],
        ),
      ],
    );

    emit(InspectionTopologyState(
      deviceName: '客房305 智能空开',
      deviceModel: 'A-301智能空开·客房301',
      topologyNodes: [rootNode],
      deviceCount: 10,
      connectionCount: 11,
    ));
  }
}
