import 'package:electronic_assistant/base/base_page.dart';
import 'package:electronic_assistant/data/bean/agenda.dart';
import 'package:electronic_assistant/module/agenda/detail/controller.dart';
import 'package:electronic_assistant/module/talk/view.dart';
import 'package:electronic_assistant/resource/colors.gen.dart';
import 'package:electronic_assistant/resource/string.gen.dart';
import 'package:electronic_assistant/router/app_pages.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import '../../../dialog/edit_agenda_dialog.dart';
import '../../../resource/assets.gen.dart';
import '../../files/view.dart';
import '../task_item_view.dart';

class AgendaDetailPage extends BasePage<AgendaDetailController> {
  const AgendaDetailPage({super.key});

  static start(Agenda agenda) {
    Get.toNamed(RoutePath.agendaDetail, arguments: agenda);
  }

  @override
  Widget buildBody(BuildContext context) {
    return Scaffold(
        backgroundColor: "#F6F6F6".toColor(),
        appBar: AppBar(
          systemOverlayStyle: SystemUiOverlayStyle.dark,
          backgroundColor: Colors.transparent,
          title: Text(
            StringName.agendaDetailTitle.tr,
            style: TextStyle(
                fontSize: 17.sp,
                fontWeight: FontWeight.bold,
                color: ColorName.primaryTextColor),
          ),
          centerTitle: true,
          leading: IconButton(
            icon: SizedBox(
                width: 24.w,
                height: 24.w,
                child: Assets.images.iconBack.image()),
            // Custom icon
            onPressed: () {
              Get.back();
            },
          ),
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildAgendaDetail(),
            SizedBox(height: 12.h),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 12.w),
              child: Text(StringName.agendaDetailFromThatTalk.tr,
                  style: TextStyle(
                      fontSize: 14.sp, color: ColorName.secondaryTextColor)),
            ),
            SizedBox(height: 12.h),
            _buildTalkDetail(),
            const Spacer(),
            _buildAnalyseButton(),
            SizedBox(height: 26.h),
          ],
        ));
  }

  Widget _buildAnalyseButton() {
    return GestureDetector(
      onTap: () {
        controller.onGoChatClick();
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 12.h),
        margin: EdgeInsets.symmetric(horizontal: 12.w),
        width: double.infinity,
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors: ['#6177F2'.toColor(), '#8B9DFF'.toColor()],
            stops: const [0, 1.0],
          ),
          borderRadius: BorderRadius.circular(8),
        ),
        child: Align(
          child: Text(StringName.homeTalkThinking.tr,
              style: TextStyle(fontSize: 16.sp, color: ColorName.white)),
        ),
      ),
    );
  }

  Widget _buildAgendaDetail() {
    if (controller.detailBean.value == null) {
      return Container();
    }
    return Builder(builder: (context) {
      return GestureDetector(
        onTap: () {
          showEditAgendaDialog(context, controller.etFieldController,
              hintTxt: controller.detailBean.value?.content,
              callback: (newContent) {
            Get.back();
            controller.onEditAgenda(newContent);
          });
        },
        child: Obx(() {
          return taskItemView(controller.detailBean.value!,
              isShowAnalyse: false, isLimitLines: false, onCheckClick: () {
            controller.onTaskDoneClick();
          });
        }),
      );
    });
  }

  @override
  Color backgroundColor() {
    return "#F6F6F6".toColor();
  }

  Widget _buildTalkDetail() {
    return Obx(() {
      if (controller.talkBean.value == null) {
        return Container();
      }
      return Padding(
        padding: const EdgeInsets.symmetric(horizontal: 12).w,
        child: buildFileTalkItem(controller.talkBean.value!, onTap: () {
          TalkPage.start(controller.talkBean.value!);
        }),
      );
    });
  }
}
