import 'package:flutter/material.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/urm_chart_panel/widgets/chart_painter.dart';
import 'package:get/get.dart';
import 'index.dart';

class URMChartPanel extends GetView<URMChartPanelController> {
  const URMChartPanel({
    Key? key,
    required this.initController,
    required this.tabHashCode,
  }) : super(key: key);

  @override
  String? get tag => tabHashCode;

  final String tabHashCode;

  final URMChartPanelController initController;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<URMChartPanelController>(
      init: initController,
      tag: tabHashCode,
      id: URMChartPanelController.urmChartPanelId,
      builder: (_) {
        if (!controller.enable) {
          return Container();
        }
        return Row(
          children: [
            SizedBox(width: 40),
            Expanded(
              child: Container(
                width: double.maxFinite,
                height: 200,
                color: Colors.black,
                child: ChartPainterWidget(
                  points: controller.displayPoints,
                  peakPoints: controller.displayPeakPoints,
                  xAxises: controller.xAxises,
                  yAxises: controller.yAxises,
                  lineColor: controller.displayLineColor,
                ),
              ),
            ),
            Container(
              width: 150,
              height: 200,
              child: Column(
                children: [
                  SizedBox(height: 20),
                  Expanded(
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Column(
                          children:
                              controller.lines.asMap().entries.map((entry) {
                            return buildSwitchButton(
                              entry.key,
                              entry.value.featureId.toString(),
                              URMChartPanelController.colorsConfig[entry.key],
                            );
                          }).toList(),
                        ),
                        Expanded(child: Container()),
                        buildIndicator(Colors.red, 'Max'),
                        SizedBox(width: 5),
                        buildIndicator(Colors.purple, 'Min'),
                        SizedBox(width: 15),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ],
        );
      },
    );
  }

  // 构建数据切换按钮: 左侧显示数字id，右侧矩形按钮，按下触发切换数据为 index
  Widget buildSwitchButton(int index, String id, Color color) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          width: 15,
          child: Text(
            id,
            textAlign: TextAlign.right,
            style: TextStyle(
              color: Colors.yellow,
              fontSize: 12,
              height: 1,
            ),
          ),
        ),
        SizedBox(
          width: 5,
          height: 20,
        ),
        InkWell(
          onTap: () => controller.setDisplayChart(index),
          child: Container(
            width: 25,
            height: 10,
            decoration: BoxDecoration(
              color: color,
            ),
          ),
        ),
      ],
    );
  }

  // 构建指示器
  Widget buildIndicator(Color color, String text) {
    return Row(
      children: [
        Container(
          width: 10,
          height: 10,
          decoration: BoxDecoration(
            color: color,
            borderRadius: BorderRadius.circular(10),
          ),
        ),
        SizedBox(width: 5),
        Text(
          text,
          style: TextStyle(
            color: Colors.yellow,
            fontSize: 12,
            height: 1,
          ),
        ),
      ],
    );
  }
}
