import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/components/rect_icon_button.dart';
import 'package:flyinsono/lab/pages/lab_import/controller.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:flyinsono/pages/schedule/calendar/utils/calendar_utils.dart';
import 'package:get/get.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_jsonrpc/rpc.dart';

class DataSourceAndTargetPanel extends GetView<LabImportController> {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabImportController>(
      id: LabImportController.sourceAndTargetPanelId,
      builder: (_) {
        return Container(
          width: double.maxFinite,
          height: 60,
          padding: EdgeInsets.symmetric(horizontal: 15),
          decoration: LabBoxDecoration.base,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Expanded(
                child: Align(
                  alignment: Alignment.centerLeft,
                  child: Container(
                    constraints: BoxConstraints(
                      maxWidth: 600,
                    ),
                    child: _buildDataSourceSelector(),
                  ),
                ),
              ),
              SizedBox(
                width: 30,
              ),
              Expanded(
                child: Align(
                  alignment: Alignment.centerRight,
                  child: Container(
                    constraints: BoxConstraints(
                      maxWidth: 600,
                    ),
                    child: _buildDataTargetSelector(),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildDataSourceSelector() {
    return Obx(() => 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.selectedDataSource,
                itemHeight: 50,
                dropdownColor: LabColors.base100,
                icon: Icon(
                  Icons.keyboard_arrow_down_rounded,
                  color: LabColors.base800,
                ),
                style: TextStyle(
                  fontSize: 18,
                  color: LabColors.text800,
                  fontFamily: FTheme.ins.localeSetting.fontFamily,
                ),
                isExpanded: true,
                elevation: 1,
                items: controller.dataSourceList
                    .map((UDiskDriveInfoDTO dataSource) {
                  return DropdownMenuItem<String>(
                    value: dataSource.path,
                    child: Text(
                      dataSource.displayName!.breakWord(),
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                    ),
                  );
                }).toList(),
                onChanged: (String? value) async {
                  if (value != null) {
                    controller.changeDataSource(value);
                  }
                },
              ),
            ),
            controller.state.isSourceListUpdating
                ? 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.refreshDataSourceList,
                  ),
          ],
        ));
  }

  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,
            ),
            value: controller.selectedDataTarget,
            itemHeight: 50,
            dropdownColor: LabColors.base100,
            icon: Icon(
              Icons.keyboard_arrow_down_rounded,
              color: LabColors.base800,
            ),
            style: TextStyle(
              fontSize: 18,
              color: LabColors.text800,
              fontFamily: FTheme.ins.localeSetting.fontFamily,
            ),
            isExpanded: true,
            elevation: 1,
            items: controller.dataTargetList
                .map((ResearchProjectBaseDTO dataSource) {
              return DropdownMenuItem<String>(
                value: dataSource.code,
                child: Text(
                  dataSource.name!.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(1.0),
                      child: CircularProgressIndicator(
                        strokeWidth: 2.5,
                        color: LabColors.base600,
                      ),
                    ),
                  ),
                )
              : RectIconButton(
                  icon: Icons.refresh_rounded,
                  tooltipMessage: "刷新",
                  onPressed: controller.updateTargetList,
                ),
        ),
      ],
    );
  }
}
