import 'package:electronic_assistant/base/base_page.dart';
import 'package:electronic_assistant/data/bean/agenda.dart';
import 'package:electronic_assistant/data/repositories/agenda_repository.dart';
import 'package:electronic_assistant/module/home/common_view.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import '../../../data/consts/Constants.dart';
import '../../../resource/colors.gen.dart';
import '../../../resource/string.gen.dart';
import '../../agenda/task_item_view.dart';
import '../../chat/view.dart';
import 'controller.dart';

class HomeAgendaView extends BasePage<HomeAgendaController> {
  const HomeAgendaView({super.key});

  @override
  bool immersive() {
    return true;
  }

  @override
  Color backgroundColor() {
    return '#F6F5F8'.color;
  }

  @override
  Widget buildBody(BuildContext context) {
    return Obx(() {
      return SmartRefresher(
          controller: controller.refreshController,
          enablePullUp: false,
          onRefresh: controller.onRefresh,
          enablePullDown: true,
          child: ListView(
            padding: EdgeInsets.only(top: 4.w),
            children: [
              for (var value in controller.agendaList.length >
                      AgendaRepository.homeMaxShowCount
                  ? controller.agendaList
                      .sublist(0, AgendaRepository.homeMaxShowCount)
                  : controller.agendaList)
                _buildAgendaView(value),
              _buildSeeMoreView()
            ],
          ));
    });
  }

  Widget _buildAgendaView(Agenda item) {
    return GestureDetector(
        onTap: () {
          controller.onAgendaItemClick(item);
        },
        child: taskItemView(
          item,
          onThinkingClick: () {
            ChatPage.startByTalkId(
                item.isExample == true
                    ? ChatFromType.fromTalkExample
                    : ChatFromType.fromAnalysisBtn,
                item.talkId,
                agenda: item);
          },
          onCheckClick: () {
            controller.agendaComplete(item);
          },
        ));
  }

  Widget _buildSeeMoreView() {
    return Obx(() {
      if (controller.agendaList.isNotEmpty) {
        return Container(
          alignment: Alignment.center,
          padding: EdgeInsets.only(
              top: 16.w, bottom: 100.w + Constants.bottomBarHeight),
          child: _buildMoreView(),
        );
      } else {
        return homeEmptyView(StringName.homeAgendaNoData.tr, _buildMoreView());
      }
    });
  }

  Widget _buildMoreView() {
    return RichText(
      text: TextSpan(
        text: StringName.homeTalkTodo1.tr,
        style: TextStyle(color: ColorName.secondaryTextColor, fontSize: 12.sp),
        children: <TextSpan>[
          TextSpan(
              text: StringName.homeTalkTodo2.tr,
              style: TextStyle(color: ColorName.colorPrimary, fontSize: 12.sp),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  controller.onGoAgendaSeeAll();
                }),
        ],
      ),
    );
  }
}
