import 'package:electronic_assistant/base/base_page.dart';
import 'package:electronic_assistant/resource/colors.gen.dart';
import 'package:electronic_assistant/resource/string.gen.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../data/bean/agenda.dart';
import '../../../data/bean/talks.dart';
import '../../../resource/assets.gen.dart';
import '../../../utils/common_style.dart';
import '../../home/view.dart';
import '../common_view.dart';
import 'controller.dart';

class TodoView extends BasePage<TodoController> {
  final String? talkId;

  TodoView(this.talkId, {super.key}) {
    Get.lazyPut(() => TodoController(talkId), tag: talkId);
  }

  @override
  get controller => Get.find<TodoController>(tag: talkId);

  @override
  Widget buildBody(BuildContext context) {
    return Obx(() {
      return _buildTodoStatusView();
    });
  }

  @override
  bool immersive() {
    return true;
  }

  _buildAllTaskView() {
    return Padding(
      padding: EdgeInsets.only(top: 6.h, bottom: 16.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(StringName.talkTodoAll.tr,
              style: TextStyle(
                  fontSize: 14.sp, color: ColorName.secondaryTextColor)),
          SizedBox(height: 12.h),
          _buildAddAgendaView(),
          buildAllTodoView(),
        ],
      ),
    );
  }

  Widget buildAllTodoView() {
    return Obx(() {
      return Column(
          children: controller.agendaAllList.map((agenda) {
        return Padding(
          padding: EdgeInsets.only(bottom: 8.h),
          child: controller.isEditModel
              ? getTodoItemView(
                  agenda,
                  (list) => getTalkAgendaEditModelList(list,
                          removeCallback: (agenda) {
                        controller.removeTalkAgenda(list, agenda);
                      }))
              : getTodoItemView(
                  agenda,
                  (list) => getTalkAgendaSettingList(list, itemClick: (agenda) {
                        controller.agendaTodo(agenda);
                      })),
        );
      }).toList());
    });
  }

  Widget _buildMineTodoList() {
    return Builder(builder: (context) {
      return Theme(
        data: Theme.of(context).copyWith(
          splashFactory: NoSplash.splashFactory,
        ),
        child: Obx(() {
          return Visibility(
            visible: !controller.isEditModel,
            child: ExpansionTile(
              initiallyExpanded: true,
              onExpansionChanged: (value) {
                controller.mineAgendaIsExpanded.value = value;
              },
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(4.0),
                side: const BorderSide(color: Colors.transparent),
              ),
              collapsedShape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(4.0),
                side: const BorderSide(color: Colors.transparent),
              ),
              minTileHeight: 46.h,
              showTrailingIcon: false,
              childrenPadding: EdgeInsets.zero,
              tilePadding: EdgeInsets.zero,
              title: Row(
                children: [
                  Text(StringName.talkTabMyTask.tr,
                      style: TextStyle(
                          fontSize: 14.sp,
                          color: ColorName.secondaryTextColor)),
                  const Spacer(),
                  Obx(() {
                    return Row(
                      children: [
                        Text(
                          '${controller.agendaMineList.length}${StringName.talkTodoItem.tr}',
                          style: TextStyle(
                              fontSize: 15.sp,
                              color: ColorName.secondaryTextColor),
                        ),
                        SizedBox(width: 3.w),
                        SizedBox(
                            width: 16.w,
                            height: 16.w,
                            child: controller.mineAgendaIsExpanded.value
                                ? Assets.images.iconTalkExpand.image()
                                : Assets.images.iconTalkCollapse.image()),
                      ],
                    );
                  }),
                  SizedBox(width: 12.w),
                ],
              ),
              children: controller.agendaMineList.map((agenda) {
                return _buildMineItem(agenda);
              }).toList(),
            ),
          );
        }),
      );
    });
  }

  Widget _buildMineItem(Agenda agenda) {
    return Container(
      margin: EdgeInsets.only(bottom: 8.w),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8.0),
        border: Border.all(
          color: '#F6F6F6'.toColor(),
          width: 2,
        ),
      ),
      padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 12.w),
      child: Row(
        children: [
          Expanded(
            child: Padding(
              padding: const EdgeInsets.only(right: 12).w,
              child: Text(agenda.content ?? '',
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                      fontSize: 15.sp,
                      fontWeight: FontWeight.bold,
                      color: ColorName.primaryTextColor)),
            ),
          ),
          GestureDetector(
            onTap: () {
              controller.onClickThinking(agenda);
            },
            child: Container(
              decoration: getPrimaryBtnDecoration(6),
              padding: const EdgeInsets.symmetric(horizontal: 9, vertical: 4).w,
              child: Text(
                StringName.homeTalkThinking.tr,
                style: TextStyle(fontSize: 13.sp, color: ColorName.white),
              ),
            ),
          )
        ],
      ),
    );
  }

  Widget _buildTodoStatusView() {
    if (controller.talkBean.value?.status.value == TalkStatus.analysisFail) {
      return getTalkFailView();
    } else if (controller.talkBean.value?.status.value ==
            TalkStatus.analysing ||
        controller.talkBean.value?.status.value == TalkStatus.waitAnalysis ||
        controller.talkBean.value?.status.value == TalkStatus.notAnalysis) {
      return getTalkAnalyseView(
          controller.talkController.isUploading.value == true &&
                  controller.talkController.talkBean.value?.status.value ==
                      TalkStatus.notAnalysis
              ? StringName.talkUploadingFileTip.tr
              : controller
                      .talkController.talkBean.value?.progressContent.value ??
                  '',
          controller.talkController.isUploading.value == true &&
                  controller.talkController.talkBean.value?.status.value ==
                      TalkStatus.notAnalysis
              ? controller.talkController.uploadProgress.value
              : controller.talkController.talkBean.value?.progress.value
                      ?.toDouble() ??
                  0.0);
    } else if (controller.talkBean.value?.status.value ==
        TalkStatus.analysisSuccess) {
      return _buildTodoContent();
    } else {
      return Container();
    }
  }

  Widget _buildTodoContent() {
    if (controller.agendaAllList.isEmpty && !controller.isEditModel) {
      return buildAgendaEmptyView(110.h);
    }
    return ListView(
      padding: EdgeInsets.only(left: 12.w, right: 12.w, bottom: 150.w),
      children: [_buildMineTodoList(), _buildAllTaskView()],
    );
  }

  Widget _buildAddAgendaView() {
    return Obx(() {
      if (controller.isEditModel) {
        return Container(
          margin: EdgeInsets.only(bottom: 8.h),
          child: Builder(builder: (context) {
            return getAddAgendaView(StringName.talkAddAgendaProject.tr,
                onClick: () {
              controller.showAddAgendaDialog(context);
            });
          }),
        );
      } else {
        return Container();
      }
    });
  }
}
