import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:flyinsono/global.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/components/classify_data_selector/classify_data_selector.dart';
import 'package:flyinsono/lab/components/operate_button/mini_operate_button.dart';
import 'package:flyinsono/lab/components/pull_down_button/more_pull_down_button.dart';
import 'package:flyinsono/lab/components/pull_down_button/pull_down_button.dart';
import 'package:flyinsono/lab/pages/lab_project/widgets/widgets.dart';
import 'package:get/get.dart';
import 'index.dart';

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

  // 主视图
  Widget _buildView() {
    return Row(
      children: [
        _buildLeftPart(),
        Expanded(
          child: LayoutBuilder(builder: (_, constraints) {
            final bool rightAreaTooSmall = constraints.maxWidth < 800;
            final bool isCollapseTimeLine = controller.isCollapseTimeLine;
            final hideTimeLine = rightAreaTooSmall || isCollapseTimeLine;
            return Row(
              children: [
                Expanded(
                  child: controller.projectClassifyData.length == 0 ||
                          controller.currentProjectList.length == 0
                      ? _buildNoDataTip()
                      : _buildMainContent(),
                ),
                // FIXME 如果需要保持右侧组件的状态，最好将其中的 scrollerController 提升至父级的 controller
                if (hideTimeLine)
                  SizedBox(
                    width: 10,
                  )
                else
                  _buildRightPart()
              ],
            );
          }),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabProjectController>(
      init: LabProjectController(
        tabHashCode: this.hashCode,
      ),
      builder: (_) {
        return Scaffold(
          backgroundColor: LabColors.base300,
          body: SafeArea(
            child: GetBuilder<LabProjectController>(
              id: LabProjectController.labMainContent,
              builder: (_) {
                return _buildView();
              },
            ),
          ),
        );
      },
    );
  }

  Widget _buildLeftPart() {
    return Container(
      width: 300,
      padding: EdgeInsets.all(10),
      child: Column(
        children: [
          Expanded(
            child: GetBuilder<LabProjectController>(
              id: LabProjectController.labProjectListId,
              builder: (_) {
                return ClassifyDataSelector(
                  loading: controller.isProjectListLoading,
                  selectedClassifyIndex: controller.selectedClassifyIndex,
                  selectedItemIndex: controller.selectedProjectIndex,
                  classifyData: controller.projectClassifyData,
                  onItemClick: (classifyIndex, itemIndex) {
                    controller.onProjectClick(classifyIndex, itemIndex);
                  },
                  actionButton: () {
                    return MiniOperateButton(
                      iconData: Icons.create_new_folder_outlined,
                      operateName: '创建项目',
                      size: 25,
                      onClick: () {
                        if (!controller.isCanCreateProject && !kDebugMode) {
                          PromptBox.toast("暂无创建项目的权限，麻烦先添加该权限");
                          return;
                        }
                        controller.createOrUpdateResearchProject(true);
                      },
                    );
                  },
                  itemActionButton: () {
                    return Container(
                      width: 24,
                      height: 24,
                      alignment: Alignment.center,
                      child: PullDownButton(
                        child: Container(
                          width: 80,
                          child: Icon(
                            Icons.more_horiz_rounded,
                            size: 24,
                            color: LabColors.base800,
                          ),
                        ),
                        iconData: Icons.file_download_outlined,
                        onClick: () {},
                        moreOperateButtons: [
                          MorePullDownButton(
                            icon: Icons.edit_outlined,
                            name: '编辑项目',
                            onClick: () {
                              controller.createOrUpdateResearchProject(false);
                            },
                          ),
                          MorePullDownButton(
                            icon: Icons.delete_outlined,
                            name: '删除项目',
                            onClick: () {
                              controller.deleteResearchProject();
                            },
                          ),
                        ],
                      ),
                    );
                  },
                );
              },
            ),
          )
        ],
      ),
    );
  }

  Widget _buildMainContent() {
    return Container(
      height: double.infinity,
      padding: EdgeInsets.symmetric(vertical: 10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          // ProjectTitle(isShowPullDownButton: controller.isShowPullDownButton),
          // SizedBox(height: 10),
          ProjectMember(),
          SizedBox(height: 10),
          Expanded(
            child: ProjectData(),
          ),
        ],
      ),
    );
  }

  // 无数据时的提示
  Widget _buildNoDataTip() {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            height: 200,
            child: ColorFiltered(
              colorFilter: ColorFilter.mode(LabColors.base400, BlendMode.srcIn),
              child: Image.asset(
                Global.isVStationSlave
                    ? "assets/images/logo_icon_0.png"
                    : "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,
          ),
          Column(
            children: [
              Text(
                "请先创建项目",
                style: TextStyle(
                  color: LabColors.text700,
                  fontSize: 14,
                ),
              ),
              SizedBox(
                height: 14,
              ),
              Container(
                // width: 120,
                height: 40,
                child: ElevatedButton.icon(
                  icon: const Icon(Icons.add),
                  style: ElevatedButton.styleFrom(
                    elevation: 0.0,
                    backgroundColor: LabColors.buttonColor,
                  ),
                  // label: const Text("刷新"),
                  label: const Text("创建项目"),
                  onPressed: () {
                    if (!controller.isCanCreateProject && !kDebugMode) {
                      PromptBox.toast("暂无创建项目的权限，麻烦先添加该权限");
                      return;
                    }
                    controller.createOrUpdateResearchProject(true);
                  },
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildRightPart() {
    if (controller.isCollapseTimeLine ||
        controller.projectClassifyData.length == 0 ||
        controller.currentProjectList.length == 0) {
      return Container();
    }
    return Container(
      width: 350,
      height: double.infinity,
      padding: EdgeInsets.all(10),
      child: ProjectTimeLine(),
    );
  }
}
