import 'dart:ui' as ui;
import 'package:fis_measure/interfaces/process/calculators/output.dart';
import 'package:fis_measure/interfaces/process/workspace/application.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class MeasureResultPanel extends StatefulWidget {
  const MeasureResultPanel({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _MeasureResultPanelState();
}

class _MeasureResultPanelState extends State<MeasureResultPanel> {
  final _scrollController = ScrollController();
  final application = Get.find<IApplication>();

  final List<OutputItem> outputs = [];

  bool get hasOutputs => outputs.isNotEmpty;

  @override
  void initState() {
    _updateOutputs();
    application.measureRerenderReady.addListener(_onMeasureRerenderReady);
    super.initState();
  }

  @override
  void dispose() {
    application.measureRerenderReady.removeListener(_onMeasureRerenderReady);
    super.dispose();
  }

  void _updateOutputs() {
    outputs.clear();
    for (var item in application.items) {
      if (item.calculator != null) {
        outputs.addAll(item.calculator!.outputs);
      }
    }
  }

  void _onMeasureRerenderReady(Object sender, void e) {
    if (mounted) {
      setState(() {
        _updateOutputs();
      });
    }
  }

  List<Widget> _buildList() {
    final list = <Widget>[
      Text(
        application.applicationName,
        style: TextStyle(color: Colors.yellow),
      ),
      const Divider(
        color: Colors.white,
        height: 0.5,
        // indent: 4,
        // endIndent: 4,
      ),
    ];
    for (var item in outputs) {
      list.add(const SizedBox(height: 4));
      list.add(_buildLine(item.description));
    }
    return list;
  }

  Widget _buildLine(String text) {
    return Text(
      text,
      style: TextStyle(color: Colors.yellow),
    );
  }

  @override
  Widget build(BuildContext context) {
    if (!hasOutputs) return const SizedBox();

    return Container(
      constraints: const BoxConstraints(
        maxWidth: 200,
        minWidth: 150,
        maxHeight: 400,
      ),
      decoration: BoxDecoration(
        border: Border.all(
          color: const Color.fromARGB(255, 79, 79, 79),
        ),
      ),
      padding: const EdgeInsets.all(8),
      child: Theme(
        data: Theme.of(context).copyWith(
          scrollbarTheme: ScrollbarThemeData(
            thumbColor: MaterialStateProperty.all(Colors.grey),
            crossAxisMargin: 1,
          ),
        ),
        child: Scrollbar(
          controller: _scrollController,
          isAlwaysShown: true,
          radius: ui.Radius.zero,
          child: ListView(
            controller: _scrollController,
            children: _buildList(),
          ),
          // child: SingleChildScrollView(
          //   physics: const AlwaysScrollableScrollPhysics(),
          //   controller: _scrollController,
          //   child: SizedBox(
          //     height: 600,
          //     child: RepaintBoundary(
          //       child: CustomPaint(
          //         painter: _ResultPainter(widget.controller.records),
          //       ),
          //     ),
          //   ),
          // ),
        ),
      ),
    );
  }
}
