import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/global.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/components/data_container/data_grid_item.dart';
import 'package:flyinsono/lab/components/data_container/data_list_item.dart';
import 'package:flyinsono/lab/components/fixed_height_grid_delegate.dart';
import 'package:flyinsono/lab/components/lab_loading_wrapper.dart';
import 'package:flyinsono/lab/components/view_mode_switcher/view_mode_switcher.dart';
import 'package:flyinsono/lab/manager/page_manager.dart';
import 'package:flyinsono/lab/pages/lab_project/controller.dart';
import 'package:flyinsono/lab/router/lab_route_names.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:get/get.dart';
import 'package:fis_common/index.dart';

class ProjectData extends GetView<LabProjectController> {
  const ProjectData({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabProjectController>(
        id: LabProjectController.projectDataPanelId,
        builder: (context) {
          return Container(
            decoration: LabBoxDecoration.base,
            child: LabLoadingWrapper(
              decoration: LabBoxDecoration.base,
              loading: controller.isProjectDataLoading,
              child: Padding(
                padding: const EdgeInsets.all(15),
                child: controller.patientData.isEmpty
                    ? _buildNoDataTip()
                    : Column(
                        children: [
                          Container(
                            height: 36,
                            child: Row(
                              children: [
                                Text(
                                  "项目数据 (${controller.patientData.length})",
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    fontSize: 16,
                                    fontWeight: FontWeight.bold,
                                    color: LabColors.text800,
                                  ),
                                ),
                                SizedBox(
                                  width: 15,
                                ),
                                Text(
                                  "排序：",
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    fontSize: 13,
                                    fontWeight: FontWeight.bold,
                                    color: LabColors.text600,
                                  ),
                                ),
                                InkWell(
                                  onTap: () {
                                    int sort = 0;
                                    if (controller.isReverse) {
                                      sort = 1;
                                    }
                                    controller.updateProjectData(
                                      controller.projectCode,
                                      sort,
                                    );
                                    controller.isReverse =
                                        !controller.isReverse;
                                  },
                                  child: Text(
                                    controller.isReverse ? "时间倒序" : "时间正序",
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                      fontSize: 13,
                                      fontWeight: FontWeight.bold,
                                      color: LabColors.text800,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  child: Container(),
                                ),
                                GetBuilder<LabProjectController>(
                                  id: LabProjectController.viewModeSwitcherId,
                                  builder: (_) {
                                    return ViewModeSwitcher(
                                      viewMode: controller.viewMode,
                                      onModeChanged: (mode) {
                                        controller.changeViewMode(mode);
                                      },
                                    );
                                  },
                                ),
                                SizedBox(
                                  width: 10,
                                ),
                                if (!FPlatform.isPureWeb || kDebugMode) ...[
                                  Container(
                                    height: 36,
                                    child: ElevatedButton.icon(
                                      icon: const Icon(
                                        Icons.add,
                                        size: 16,
                                      ),
                                      style: ElevatedButton.styleFrom(
                                        elevation: 0.0,
                                        backgroundColor: LabColors.buttonColor,
                                        padding: EdgeInsets.symmetric(
                                          horizontal: 12,
                                          vertical: 5,
                                        ),
                                      ),
                                      label: const Text(
                                        "导入数据",
                                        style: TextStyle(
                                          fontSize: 15,
                                          height: 1,
                                          color: LabColors.text100,
                                        ),
                                      ),
                                      onPressed: () {
                                        controller.clickImportData();
                                        // PageManager.ins.openNewTab(
                                        //     LabRouteNames.Import, "导入数据",
                                        //     multiple: false,
                                        //     args: controller.projectCode);
                                      },
                                    ),
                                  ),
                                ],
                              ],
                            ),
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Expanded(
                            child: GetBuilder<LabProjectController>(
                              id: LabProjectController.projectDataViewId,
                              builder: (_) {
                                if (controller.viewMode == ViewMode.list) {
                                  return _buildProjectDataInListView();
                                }
                                return _buildProjectDataInGridView();
                              },
                            ),
                          ),
                        ],
                      ),
              ),
            ),
          );
        });
  }

  // 以 3 列的形式展示项目数据
  Widget _buildProjectDataInGridView() {
    return GridView.builder(
      shrinkWrap: true,
      gridDelegate: FixedHeightGridDelegate(
        crossAxisCount: 3,
        crossAxisSpacing: 10,
        mainAxisSpacing: 10,
        height: 60, //48 dp of height
      ),
      itemCount: controller.patientData.length,
      itemBuilder: (BuildContext context, int index) {
        return _buildDataGridItem(index);
      },
    );
  }

  // 以数据行的形式展示项目数据
  Widget _buildProjectDataInListView() {
    return Column(
      children: [
        DataListTitle(title: "样品名称"),
        Expanded(
          child: ListView.builder(
            shrinkWrap: true,
            itemCount: controller.patientData.length,
            itemBuilder: (BuildContext context, int index) {
              return _buildDataListItem(index);
            },
          ),
        ),
      ],
    );
  }

  Widget _buildDataGridItem(int index) {
    return DataGridItem(
      projectDataName: controller.patientData[index].name,
      projectDataId: controller.patientData[index].id,
      projectDataFileSize: controller.patientData[index].fileSize,
      isExistUrmData: controller.patientData[index].isExistUrmData,
      onClick: () {
        controller.clickProjectDataItem(index);
      },
    );
  }

  Widget _buildDataListItem(int index) {
    return DataListItem(
      projectDataName: controller.patientData[index].name,
      projectDataId: controller.patientData[index].id,
      projectDataFileSize: controller.patientData[index].fileSize,
      isExistUrmData: controller.patientData[index].isExistUrmData,
      onClick: () {
        controller.clickProjectDataItem(index);
      },
    );
  }

  // 无数据时的提示
  Widget _buildNoDataTip() {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Global.isVStationSlave
              ? Container(
                  height: 120,
                )
              : Container(
                  height: 200,
                  child: ColorFiltered(
                    colorFilter:
                        ColorFilter.mode(LabColors.text400, BlendMode.srcIn),
                    child: Image.asset(
                      "assets/images/logo_icon.png",
                      filterQuality: FilterQuality.medium,
                      isAntiAlias: true,
                      fit: BoxFit.contain,
                    ),
                  ),
                ),
          SizedBox(
            height: 10,
          ),
          Text(
            "该项目内暂无数据",
            style: TextStyle(
              color: LabColors.text600,
              fontSize: 16,
            ),
          ),
          SizedBox(
            height: 30,
          ),
          if (!FPlatform.isPureWeb) ...[
            Column(
              children: [
                Text(
                  "请先导入数据",
                  style: TextStyle(
                    color: LabColors.text700,
                    fontSize: 14,
                  ),
                ),
                SizedBox(
                  height: 10,
                ),
                Container(
                  // width: 120,
                  height: 40,
                  child: ElevatedButton.icon(
                    icon: const Icon(Icons.file_download_outlined),
                    style: ElevatedButton.styleFrom(
                      elevation: 0.0,
                      backgroundColor: LabColors.buttonColor,
                    ),
                    label: const Text("去导入"),
                    onPressed: () {
                      controller.clickImportData();
                      // PageManager.ins.openNewTab(LabRouteNames.Import, "导入数据",
                      //     multiple: false, args: controller.projectCode);
                    },
                  ),
                ),
              ],
            ),
          ],
        ],
      ),
    );
  }
}
