import 'package:cached_network_image/cached_network_image.dart';
import 'package:clean/base/base_page.dart';
import 'package:clean/module/wallpaper/wallpaper_controller.dart';
import 'package:clean/resource/assets.gen.dart';
import 'package:clean/router/app_pages.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';

class WallPaperPage extends BasePage<WallPaperController> {
  const WallPaperPage({Key? key}) : super(key: key);

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

  @override
  bool immersive() {
    // TODO: implement immersive
    return true;
  }

  @override
  bool statusBarDarkFont() {
    return false;
  }

  @override
  @override
  Widget buildBody(BuildContext context) {
    return Stack(
      children: [
        PopScope(
          canPop: false,
          onPopInvokedWithResult: (didPop, result) {
            if (didPop) {
              return;
            }
            controller.clickBack();
          },
          child: SafeArea(
            child: SizedBox(
              width: double.infinity,
              height: double.infinity,
              child: Column(
                children: [
                  _titleCard(),
                  Expanded(
                    child: SingleChildScrollView(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            padding: EdgeInsets.symmetric(horizontal: 16.w),
                            child: Text(
                              "Wallpaper",
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 24.sp,
                                fontWeight: FontWeight.w700,
                              ),
                            ),
                          ),
                          SizedBox(height: 6.h),
                          Container(
                            padding: EdgeInsets.symmetric(horizontal: 16.w),
                            child: Obx(() {
                              return GridView.builder(
                                gridDelegate:
                                    SliverGridDelegateWithFixedCrossAxisCount(
                                  crossAxisCount: 2,
                                  childAspectRatio: 9 / 16,
                                  crossAxisSpacing: 16.w,
                                  mainAxisSpacing: 16.w,
                                ),
                                itemCount: controller.wallpapersList.length,
                                physics: NeverScrollableScrollPhysics(),
                                shrinkWrap: true,
                                itemBuilder: (context, index) {
                                  return GestureDetector(
                                      onTap: () {
                                        controller.clickWallpaper(
                                            controller.wallpapersList[index]);
                                      },
                                      child: ClipRRect(
                                        borderRadius:
                                            BorderRadius.circular(12.r),
                                        child: CachedNetworkImage(
                                          width: 156.w,
                                          height: 277.w,

                                          placeholder: (context, url) =>
                                              Assets.images.iconHomeNoPhoto
                                                  .image(
                                                width: 156.w,
                                                height: 156 .w,
                                          ),
                                          imageUrl: controller
                                                  .wallpapersList[index]
                                                  .thumbUrl ??
                                              "",
                                          errorWidget: (context, url, error) =>
                                              Icon(Icons.error),
                                          fit: BoxFit.cover,
                                        ),
                                      ));
                                },
                              );
                            }),
                          ),
                          SizedBox(height: 16.h),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
        IgnorePointer(
          child: Assets.images.bgHome.image(
            width: 360.w,

          ),
        )
      ],
    );
  }

  Widget _titleCard() {
    return Container(
      alignment: Alignment.centerLeft,
      padding:
          EdgeInsets.only(left: 16.w, top: 14.h, right: 16.w, bottom: 20.h),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          GestureDetector(
            onTap: () => controller.clickBack(),
            child: Assets.images.iconBackArrow.image(
              width: 28.w,
              height: 28.h,
            ),
          ),
        ],
      ),
    );
  }
}
