// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_measure_overlay/controller.dart';
import 'package:get/get.dart';
import 'package:fis_measure/values/colors.dart';
import 'package:fis_jsonrpc/services/remedical.m.dart';

class RimMeasureResultTable extends GetView<ImageMeasureOverlayController> {
  RimMeasureResultTable({
    Key? key,
    required this.tabHashCode,
  }) : super(key: key);

  @override
  String? get tag => tabHashCode;
  static TextStyle textStyle = TextStyle(
    color: MeasureColors.Primary,
    fontSize: 12,
  );

  final String tabHashCode;

  final _scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImageMeasureOverlayController>(
      tag: tabHashCode,
      id: ImageMeasureOverlayController.rimMeasureResultTableId,
      builder: (_) {
        if (controller.rimMeasureResultList.length == 0) return Container();
        return LayoutBuilder(builder: (context, constraints) {
          return Container(
            margin: const EdgeInsets.only(top: 0, bottom: 10),
            constraints: const BoxConstraints(
              maxWidth: 270,
              minWidth: 150,
              maxHeight: 400,
            ),
            decoration: BoxDecoration(
              border: Border.all(
                color: MeasureColors.ResultBorder,
                width: 2,
              ),
              borderRadius: BorderRadius.circular(4),
              color: Colors.black.withOpacity(0.7),
            ),
            child: SingleChildScrollView(
              controller: _scrollController,
              child: RepaintBoundary(
                child: IntrinsicHeight(
                  child: IntrinsicWidth(
                    child: Container(
                      padding: EdgeInsets.only(bottom: 4),
                      child: DataTable(
                        dataRowHeight: 15,
                        columnSpacing: 10,
                        headingRowHeight: 15,
                        showBottomBorder: true,
                        columns: [
                          DataColumn(
                            label: Text(
                              '名称',
                              style: textStyle,
                            ),
                          ),
                          DataColumn(
                              label: Row(
                            children: [
                              Container(
                                margin: EdgeInsets.only(top: 4),
                                padding: EdgeInsets.only(top: 200),
                                width: 10,
                                height: 10,
                                decoration: BoxDecoration(
                                  color: Color(0xFF2897ea).withOpacity(0.5),
                                  shape: BoxShape.rectangle,
                                ),
                              ),
                              Text(
                                'rim',
                                style: textStyle,
                              ),
                            ],
                          )),
                          DataColumn(
                            label: Row(
                              children: [
                                Container(
                                  margin: EdgeInsets.only(top: 4),
                                  width: 10,
                                  height: 10,
                                  decoration: BoxDecoration(
                                    color: Color(0xFF57ED26).withOpacity(0.5),
                                    shape: BoxShape.rectangle,
                                  ),
                                ),
                                Text(
                                  'in',
                                  style: textStyle,
                                ),
                              ],
                            ),
                          ),
                          DataColumn(
                            label: Row(
                              children: [
                                Container(
                                  margin: EdgeInsets.only(top: 4),
                                  width: 10,
                                  height: 10,
                                  decoration: BoxDecoration(
                                    color: Color(0xFF2897ea).withOpacity(0.5),
                                    shape: BoxShape.rectangle,
                                  ),
                                ),
                                Text(
                                  '+',
                                  style: textStyle,
                                ),
                                Container(
                                  margin: EdgeInsets.only(top: 4),
                                  width: 10,
                                  height: 10,
                                  decoration: BoxDecoration(
                                    color: Color(0xFF57ED26).withOpacity(0.5),
                                    shape: BoxShape.rectangle,
                                  ),
                                ),
                                Text(
                                  'out',
                                  style: textStyle,
                                ),
                              ],
                            ),
                          ),
                        ],
                        rows: controller.rimMeasureResultList
                            .map(
                              (e) => DataRow(cells: [
                                DataCell(Text(
                                  e.rowName,
                                  style: textStyle.copyWith(fontSize: 10),
                                )),
                                DataCell(Text(
                                  e.rim.toStringAsFixed(2),
                                  style: textStyle.copyWith(fontSize: 10),
                                )),
                                DataCell(Text(
                                  e.inRim.toStringAsFixed(2),
                                  style: textStyle.copyWith(fontSize: 10),
                                )),
                                DataCell(Text(
                                  e.outRim.toStringAsFixed(2),
                                  style: textStyle.copyWith(fontSize: 10),
                                )),
                              ]),
                            )
                            .toList(),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          );
        });
      },
    );
  }
}

/// 单行rim 测量结果值
class RimMeasureRowResult {
  String rowName;
  double outRim;
  double inRim;
  double rim;
  Unit unit;

  RimMeasureRowResult(
      {required this.rowName,
      required this.outRim,
      required this.inRim,
      required this.rim,
      required this.unit});
}
