import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/components/rect_icon_button.dart';
import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
import 'package:flyinsono/lab/pages/lab_export/controller.dart';
import 'package:flyinsono/lab/pages/lab_export/dialog/image_setting_dialog.dart';
import 'package:flyinsono/lab/pages/lab_export/entitys/image_type.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:get/get.dart';
import 'package:fis_theme/theme.dart';

class ExportConfigPanel extends GetView<LabExportController> {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabExportController>(
      id: LabExportController.exportConfigPanelId,
      builder: (_) {
        return Container(
          width: double.maxFinite,
          height: 70,
          decoration: LabBoxDecoration.base,
          child: Row(
            children: [
              SizedBox(width: 15),
              Text(
                "已选择：${controller.multipleCheckList.length} 个样本，文件：${controller.totalImageListLength}",
                style: TextStyle(
                  fontSize: 16,
                  color: LabColors.text800,
                ),
              ),
              Expanded(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    TextButton.icon(
                        onPressed: () async {
                          ExportImageFormat? result = await Get.dialog(
                            ImageSettingDialog(
                              title: "图像设置",
                              imageType: controller.exportImageFormat,
                              onConfirm: null,
                              showCancelButton: false,
                            ),
                            barrierColor: LabColors.base100.withOpacity(0.1),
                          );
                          if (result != null) {
                            controller.exportImageFormat = result;
                            Store.user.setExportImageFormat(
                                result.imageType, result.videoType);
                          }
                        },
                        icon: Icon(Icons.settings),
                        label: Text(
                          "图像设置",
                          style: TextStyle(
                            fontSize: 18,
                            color: LabColors.text800,
                          ),
                        )),
                    SizedBox(
                      width: 30,
                    ),
                    Container(
                      constraints: BoxConstraints(maxWidth: 400),
                      child: _buildDataTargetSelector(),
                    ),
                  ],
                ),
              ),
              SizedBox(width: 10),
              Container(
                height: 50,
                child: ElevatedButton.icon(
                  icon: const Icon(
                    Icons.file_upload_outlined,
                    size: 20,
                  ),
                  style: ElevatedButton.styleFrom(
                    elevation: 0.0,
                    backgroundColor: LabColors.buttonColor,
                    padding: EdgeInsets.symmetric(
                      horizontal: 40,
                      vertical: 10,
                    ),
                  ),
                  label: const Text(
                    "导出",
                    style: TextStyle(
                      fontSize: 16,
                      height: 1,
                      color: LabColors.text100,
                    ),
                  ),
                  onPressed: () {
                    controller.exportDataToProject();
                  },
                ),
              ),
              SizedBox(width: 15),
            ],
          ),
        );
      },
    );
  }

  Widget _buildDataTargetSelector() {
    return Row(
      children: [
        Text(
          "导出至：",
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
            color: LabColors.text800,
          ),
        ),
        Expanded(
          child: DropdownButton<String>(
            underline: Container(
              color: LabColors.base500,
              height: 1,
            ),
            hint: Text(
              "请插入U盘",
              style: TextStyle(color: LabColors.text800),
            ),
            value: controller.selectedDataTarget,
            itemHeight: 50,
            dropdownColor: LabColors.base100,
            icon: Icon(
              Icons.keyboard_arrow_down_rounded,
              color: LabColors.text800,
            ),
            style: TextStyle(
              fontSize: 18,
              color: LabColors.text800,
              fontFamily: FTheme.ins.localeSetting.fontFamily,
            ),
            isExpanded: true,
            elevation: 1,
            items:
                controller.dataTargetList.map((UDiskDriveInfoDTO dataSource) {
              return DropdownMenuItem<String>(
                value: dataSource.path,
                child: Text(
                  dataSource.displayName!.breakWord(),
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                ),
              );
            }).toList(),
            onChanged: (String? value) {
              if (value != null) {
                controller.changeDataTarget(value);
              }
            },
          ),
        ),
        Obx(
          () => controller.state.isTargetListUpdating
              ? SizedBox.square(
                  dimension: 40,
                  child: Center(
                    child: Padding(
                      padding: const EdgeInsets.all(12.0),
                      child: CircularProgressIndicator(
                        strokeWidth: 2.5,
                        color: LabColors.base600,
                      ),
                    ),
                  ),
                )
              : RectIconButton(
                  icon: Icons.refresh_rounded,
                  tooltipMessage: "刷新",
                  onPressed: controller.updateTargetList,
                ),
        ),
      ],
    );
  }
}
