import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/components/lab_overflow_tooltip_wrapper.dart';
import 'package:flyinsono/lab/components/lab_text_tool_tip.dart';
import 'package:flyinsono/lab/components/rect_icon_button.dart';
import 'package:flyinsono/lab/pages/lab_project/controller.dart';
import 'package:flyinsono/lab/pages/lab_project/widgets/project_member_item.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';

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabProjectController>(
        id: LabProjectController.projectSelectMember,
        builder: (context) {
          return Container(
            clipBehavior: Clip.hardEdge,
            decoration: LabBoxDecoration.base,
            padding: const EdgeInsets.all(5),
            child: Column(
              children: [
                // TODO 找个地方安置项目类型
                // Row(
                //   children: [
                //     SizedBox(
                //       width: 10,
                //     ),
                //     Text(
                //       "项目类型：",
                //       overflow: TextOverflow.ellipsis,
                //       style: TextStyle(
                //         fontSize: 16,
                //         fontWeight: FontWeight.bold,
                //         color: LabColors.text800,
                //       ),
                //     ),
                //     Text(
                //       controller.projectTypeName,
                //       style: TextStyle(
                //         decoration: TextDecoration.none,
                //         color: LabColors.text600,
                //         fontSize: 16,
                //       ),
                //     ),
                //   ],
                // ),
                SizedBox(
                  height: 3,
                ),
                Row(
                  children: [
                    SizedBox(
                      width: 10,
                    ),
                    Expanded(
                      child: Container(
                        height: 35,
                        child: Row(
                          children: [
                            // Text(
                            //   "项目类型：",
                            //   overflow: TextOverflow.ellipsis,
                            //   style: TextStyle(
                            //     fontSize: 16,
                            //     fontWeight: FontWeight.bold,
                            //     color: LabColors.text800,
                            //   ),
                            // ),
                            // SizedBox(
                            //   width: 5,
                            // ),
                            Text(
                              "项目简介",
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                fontSize: 16,
                                fontWeight: FontWeight.bold,
                                color: LabColors.text800,
                              ),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Padding(
                              padding: const EdgeInsets.only(top: 2),
                              child: Container(
                                decoration: BoxDecoration(
                                  color: LabColors.specialHighlightColor,
                                  borderRadius: BorderRadius.circular(5),
                                  border: Border.all(
                                    color: LabColors.base600,
                                    width: 1,
                                  ),
                                ),
                                padding: EdgeInsets.symmetric(
                                  horizontal: 4,
                                  vertical: 4,
                                ),
                                child: Text(
                                  controller.projectTypeName,
                                  style: TextStyle(
                                    fontSize: 12,
                                    height: 1,
                                    decoration: TextDecoration.none,
                                    color: LabColors.text600,
                                  ),
                                ),
                              ),
                            ),

                            Expanded(
                              child: Container(),
                            ),
                            if (controller.isShowPullDownButton)
                              Container(
                                height: 36,
                                child: ElevatedButton.icon(
                                  icon: const Icon(
                                    Icons.people_alt_rounded,
                                    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
                                        .projectMemberSearchInputController
                                        .text = "";
                                    controller.projectMemberManagement();
                                  },
                                ),
                              ),
                            if (controller.isCollapseTimeLine)
                              RectIconButton(
                                icon: Icons.access_time_rounded,
                                tooltipMessage: "时间轴",
                                onPressed: () {
                                  controller.setTimeLineCollapse(false);
                                },
                              ),
                          ],
                        ),
                      ),
                    )
                  ],
                ),
                SizedBox(
                  height: 10,
                ),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(
                      width: 10,
                    ),
                    Expanded(
                      child: projectIntroduction(
                        controller.projectIntroduction,
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 10,
                ),
              ],
            ),
          );
        });
  }

  Widget projectIntroduction(String? introduction) {
    if (introduction == null || introduction.isEmpty) {
      return Text(
        "暂未填写项目简介",
        style: TextStyle(
          fontSize: 14,
          color: LabColors.text600,
        ),
      );
    } else {
      return LabOverflowTooltipWrapper(
        position: DisplayPosition.bottom,
        offset: Offset.zero,
        child: Text(
          introduction.breakWord(),
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
          style: TextStyle(
            fontSize: 16,
            fontFamily: FTheme.ins.localeSetting.fontFamily,
            decoration: TextDecoration.none,
            color: LabColors.text600,
          ),
        ),
      );
    }
  }

  List<Widget> _buildProjectMemberList() {
    List<Widget> memberListWidget = [];
    List<MemberModel> memberList = controller.selectMemberModelList;
    bool isShowMore = controller.selectMemberModelList.length > 8;

    if (controller.selectMemberModelList.length > 8) {
      memberList = controller.selectMemberModelList.sublist(0, 8);
    }
    for (int i = 0; i < memberList.length; i++) {
      memberListWidget.add(
        ProjectMemberItem(
          member: memberList[i],
          onChangeSelectMember: (value) {},
          isSelected: false,
          imageSize: 30,
        ),
      );
    }
    if (isShowMore) {
      memberListWidget.add(_buildShowMore());
    }
    if (controller.isShowPullDownButton) {
      memberListWidget.add(_buildAddMemberButton());
    }

    return memberListWidget;
  }

  Widget _buildAddMemberButton() {
    return MouseRegion(
      cursor: SystemMouseCursors.click,
      child: GestureDetector(
        onTap: () {
          controller.projectMemberManagement();
        },
        child: Column(
          children: [
            Container(
              width: 30,
              height: 30,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(3),
                border: Border.all(
                  color: LabColors.base700,
                  width: 1,
                ),
              ),
              child: Center(
                child: RectIconButton(
                  icon: Icons.add_rounded,
                  iconSize: 20,
                  onPressed: () {
                    controller.projectMemberManagement();
                  },
                ),
              ),
            ),
            SizedBox(
              height: 5,
            ),
            Container(
              width: 50,
              child: Text(
                "邀请",
                textAlign: TextAlign.center,
                overflow: TextOverflow.ellipsis,
                maxLines: 1,
                style: TextStyle(
                  fontSize: 12,
                  color: LabColors.text800,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildShowMore() {
    return MouseRegion(
      cursor: SystemMouseCursors.click,
      child: GestureDetector(
        onTap: () {
          controller.lookProjectMember();
        },
        child: Column(
          children: [
            Container(
              width: 30,
              height: 30,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(3),
                border: Border.all(
                  color: LabColors.base700,
                  width: 1,
                ),
              ),
              child: Center(
                child: RectIconButton(
                  icon: Icons.more_horiz,
                  iconSize: 20,
                  onPressed: () {
                    controller.lookProjectMember();
                  },
                ),
              ),
            ),
            SizedBox(
              height: 5,
            ),
            Container(
              width: 50,
              child: Text(
                "查看全部",
                textAlign: TextAlign.center,
                overflow: TextOverflow.ellipsis,
                maxLines: 1,
                style: TextStyle(
                  fontSize: 12,
                  color: LabColors.text800,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
