import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:location/base/base_page.dart';
import 'package:location/data/bean/common_point_bean.dart';
import 'package:location/resource/assets.gen.dart';
import 'package:location/resource/colors.gen.dart';
import 'package:location/resource/string.gen.dart';
import 'package:location/utils/common_expand.dart';

import '../../router/app_pages.dart';
import '../../utils/common_style.dart';
import '../../widget/common_view.dart';
import 'common_point_controller.dart';

class CommonPointPage extends BasePage<CommonPointController> {
  const CommonPointPage({super.key});

  static void start() {
    Get.toNamed(RoutePath.commonPoint);
  }

  @override
  bool immersive() {
    return true;
  }

  @override
  Widget buildBody(BuildContext context) {
    return Stack(
      children: [
        Assets.images.bgPageBackground.image(width: double.infinity),
        SafeArea(
          child: Column(
            children: [buildHeaderView(), Expanded(child: buildContentView())],
          ),
        ),
        Positioned(
            right: 12.5.w,
            bottom: 141.w,
            child: Assets.images.iconCommonPointAiSummary.image(width: 60.w))
      ],
    );
  }

  Widget buildHeaderView() {
    return CommonView.buildAppBar(StringName.commonPointTitle,
        backOnTap: controller.onBack,
        rightWidget: GestureDetector(
          onTap: controller.onSettingClick,
          child: Center(
            child: Text(
              StringName.commonPointSetting,
              style: TextStyle(fontSize: 14.sp, color: ColorName.colorPrimary),
            ),
          ),
        ));
  }

  Widget buildContentView() {
    return Obx(() {
      if (controller.commonPointList.isEmpty) {
        return buildCommonPointEmptyView();
      }
      return buildCommonPointListView();
    });
  }

  Widget buildCommonPointListView() {
    return Column(
      children: [
        Expanded(
          child: ListView.separated(
              padding: EdgeInsets.only(bottom: 160.w),
              separatorBuilder: (context, index) {
                return Container(
                  height: 0.5.w,
                  margin: EdgeInsets.only(left: 16.w, right: 16.w),
                  color: ColorName.black10,
                );
              },
              itemBuilder: (BuildContext context, int index) =>
                  buildCommonPointListItem(controller.commonPointList[index]),
              itemCount: controller.commonPointList.length),
        ),
        GestureDetector(
          onTap: controller.onAddCommonPointClick,
          child: Container(
            margin: EdgeInsets.only(
                top: 15.w, left: 15.w, right: 15.w, bottom: 12.w),
            decoration: getPrimaryBtnDecoration(10.r),
            width: double.infinity,
            height: 46.w,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Assets.images.iconCommonPointAdd
                    .image(width: 14.w, height: 14.w),
                SizedBox(width: 2.w),
                Text(StringName.commonPointAdd,
                    style: TextStyle(fontSize: 14.sp, color: ColorName.white))
              ],
            ),
          ),
        )
      ],
    );
  }

  Widget buildCommonPointEmptyView() {
    return Center(
      child: IntrinsicHeight(
        child: Column(
          children: [
            Assets.images.iconCommonPointNoData.image(width: 157.w),
            SizedBox(height: 12.8.w),
            Text(StringName.commonPointNoData,
                style: TextStyle(fontSize: 13.sp, color: ColorName.black60)),
            SizedBox(height: 36.w),
            GestureDetector(
              onTap: controller.onAddCommonPointClick,
              child: Container(
                decoration: getPrimaryBtnDecoration(10.r),
                width: 136.w,
                height: 46.w,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Assets.images.iconCommonPointAdd
                        .image(width: 14.w, height: 14.w),
                    SizedBox(width: 2.w),
                    Text(StringName.commonPointAdd,
                        style:
                            TextStyle(fontSize: 14.sp, color: ColorName.white))
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget buildCommonPointListItem(CommonPointBean item) {
    return Slidable(
        // Specify a key if the Slidable is dismissible.
        key: ValueKey(item),

        // The end action pane is the one at the right or the bottom side.
        endActionPane: ActionPane(
          extentRatio: 0.1666666666666667,
          motion: ScrollMotion(),
          children: [
            CustomSlidableAction(
                padding: EdgeInsets.zero,
                borderRadius: BorderRadius.circular(10.r),
                backgroundColor: '#FF7272'.color,
                onPressed: (ctx) {},
                child: Text(StringName.commonPointDelete,
                    style: TextStyle(fontSize: 12.sp, color: Colors.white)))
          ],
        ),
        child: buildListItem(item));
  }

  Widget buildListItem(CommonPointBean item) {
    return SizedBox(
      width: double.infinity,
      height: 70.w,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(width: 16.w),
          Container(
              width: 40.w,
              height: 40.w,
              decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: ColorName.colorPrimary.withOpacity(0.1)),
              child: Center(
                child: Text(
                  item.title.substring(0, 1),
                  style: TextStyle(
                      fontSize: 16.sp,
                      color: ColorName.colorPrimary,
                      fontWeight: FontWeight.bold),
                ),
              )),
          SizedBox(width: 12.w),
          Expanded(
              child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(item.title,
                  style: TextStyle(fontSize: 14.sp, color: ColorName.black90)),
              SizedBox(height: 6.w),
              Text(item.address ?? '',
                  style: TextStyle(
                      overflow: TextOverflow.ellipsis,
                      fontSize: 12.sp,
                      color: ColorName.black30))
            ],
          )),
          SizedBox(width: 16.w),
        ],
      ),
    );
  }
}
