import 'package:electronic_assistant/base/base_page.dart';
import 'package:electronic_assistant/router/app_pages.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:flutter/cupertino.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:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import '../../../data/bean/template_bean.dart';
import '../../../popup/common_popup.dart';
import '../../../popup/template_utils.dart';
import '../../../resource/assets.gen.dart';
import '../../../resource/colors.gen.dart';
import '../../../resource/string.gen.dart';
import '../../../utils/common_style.dart';
import 'controller.dart';

class TemplateListPage extends BasePage<TemplateListController> {
  const TemplateListPage({super.key});

  static Future<dynamic> start() async {
    return Get.toNamed(RoutePath.templateList);
  }

  @override
  bool immersive() {
    return true;
  }

  @override
  Widget buildBody(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        controller.onBack();
        return false;
      },
      child: Scaffold(
        backgroundColor: '#F6F6F6'.color,
        appBar: _buildAppBar(),
        body: Column(
          children: [
            Expanded(child: _buildTemplateList()),
            _buildTemplateBtn()
          ],
        ),
      ),
    );
  }

  Widget _buildTemplateBtn() {
    return GestureDetector(
      onTap: () {
        controller.onAddTemplate();
      },
      child: Center(
        child: Container(
          margin: EdgeInsets.only(bottom: 16.w),
          decoration: getCommonDecoration(8.w),
          width: 328.w,
          height: 48.w,
          child: Center(
            child: Text(
              StringName.talkAddTemplate.tr,
              style: TextStyle(fontSize: 16.sp, color: ColorName.white),
            ),
          ),
        ),
      ),
    );
  }

  AppBar _buildAppBar() {
    return AppBar(
      scrolledUnderElevation: 0,
      systemOverlayStyle: SystemUiOverlayStyle.dark,
      backgroundColor: Colors.transparent,
      title: Text(
        StringName.templateManageTitle.tr,
        style: TextStyle(fontSize: 17.sp, color: ColorName.primaryTextColor),
      ),
      centerTitle: true,
      leading: IconButton(
          onPressed: () {
            controller.onBack();
          },
          icon: SizedBox(
              width: 24.w,
              height: 24.w,
              child: Assets.images.iconBack.image())),
    );
  }

  Widget _buildTemplateList() {
    return Obx(() {
      return ListView.builder(
        padding: EdgeInsets.only(top: 12.w),
        itemCount: controller.templateList.length,
        itemBuilder: (context, index) {
          return _buildTemplateItem(controller.templateList[index]);
        },
      );
    });
  }

  Widget _buildTemplateItem(TemplateBean templateBean) {
    return GestureDetector(
      onTap: () {
        controller.onDetailClick(templateBean);
      },
      child: Container(
        decoration: BoxDecoration(
            color: ColorName.white, borderRadius: BorderRadius.circular(8.w)),
        padding: EdgeInsets.only(left: 16.w, right: 12.w),
        margin: EdgeInsets.only(bottom: 8.h, left: 12.w, right: 12.w),
        child: Row(
          children: [
            Padding(
              padding: EdgeInsets.symmetric(vertical: 20.w),
              child: Text(templateBean.name ?? '',
                  style: TextStyle(
                      fontSize: 15.sp,
                      color: ColorName.primaryTextColor,
                      height: 1)),
            ),
            const Spacer(),
            Visibility(
                visible: !templateBean.isDefaultTemp,
                child: GestureDetector(
                  onTapUp: (details) {
                    showPressTouchPopup(
                        details.globalPosition + Offset(-40.w, 20.w),
                        Alignment.bottomLeft,
                        _buildPopupView(templateBean, 'templateMorePopup'),
                        tag: 'templateMorePopup');
                  },
                  child: Assets.images.iconTemplateMoreOperation
                      .image(width: 24.w, height: 24.w),
                )),
          ],
        ),
      ),
    );
  }

  List<Widget> _buildPopupView(TemplateBean item, String tag) {
    return [
      createNormalPopupItem(StringName.agendaDetailPopupUpdate.tr,
          onItemClick: () {
        SmartDialog.dismiss(tag: tag);
        controller.templateEdit(item);
      }),
      createPopupDivider(),
      createDeletePopupItem(() {
        SmartDialog.dismiss(tag: tag);
        controller.onDeleteTemplate(item);
      }),
    ];
  }
}
