import 'dart:io';
import 'dart:ui';

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:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:location/base/base_page.dart';
import 'package:location/utils/common_expand.dart';
import '../../resource/assets.gen.dart';
import '../../resource/colors.gen.dart';
import '../../resource/string.gen.dart';
import '../../router/app_pages.dart';
import '../../widget/common_view.dart';
import 'news_controller.dart';
import 'news_list_item.dart';

class NewsPage extends BasePage<NewsController> {
  const NewsPage({super.key});

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

  @override
  bool immersive() {
    return true;
  }

  @override
  Widget buildBody(BuildContext context) {
    return Stack(
      children: [
        Assets.images.bgPageBackground.image(width: double.infinity),
        Positioned(
            top: 24.w,
            right: 19.w,
            child: SafeArea(child: Obx(() {
              return Visibility(
                visible: controller.waitingNewsCount > 0 ||
                    controller.messageList.isNotEmpty,
                child: Opacity(
                    opacity: 1 - controller.opacity,
                    child: Assets.images.iconNews.image(width: 118.w)),
              );
            }))),
        SafeArea(
          child: Column(
            children: [
              buildHeadView(),
              Obx(() {
                return Expanded(
                    child: controller.waitingNewsCount == 0 &&
                            controller.messageList.isEmpty
                        ? buildNoMessageView()
                        : buildHasMessageView());
              }),
              SizedBox(
                height:18.w + 46.w + 10.w,
              )
            ],
          ),
        ),
        Positioned(
            bottom: MediaQuery.of(Get.context!).padding.bottom + 18.w,
            left: 0,
            right: 0,
            height: 46.w,
            child: GestureDetector(
              onTap: () {
                controller.onAddJumpToAddNewFriend();
              },
              child: Container(
                decoration: BoxDecoration(
                  color: "#7B7DFF".color,
                  borderRadius: BorderRadius.circular(10.w),
                ),
                margin: EdgeInsets.symmetric(horizontal: 15.w),
                height: 46.w,
                alignment: Alignment.center,
                child: Text("添加好友",
                    style: TextStyle(
                        fontSize: 14.sp,
                        color: '#FFFFFF'.color,
                        fontWeight: FontWeight.w500)
                ),
              ),
            )),
      ],
    );
  }

  CustomScrollView buildHasMessageView() {
    return CustomScrollView(
      controller: controller.scrollController,
      slivers: [
        buildSliverTitle(),
        buildSliverWaiting(),
        Obx(() {
          return SliverList.builder(
              itemBuilder: buildNewsItem,
              itemCount: controller.messageList.length);
        })
      ],
    );
  }

  SliverToBoxAdapter buildSliverTitle() {
    return SliverToBoxAdapter(
        child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(height: 18.w),
        Padding(
          padding: EdgeInsets.only(left: 12.w),
          child: Text(StringName.newsTitle,
              style: TextStyle(
                  fontSize: 16.sp,
                  color: ColorName.black90,
                  fontWeight: FontWeight.bold)),
        ),
        SizedBox(height: 10.w),
      ],
    ));
  }

  Widget buildHeadView() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 12.w, vertical: 14.w),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          GestureDetector(
              onTap: controller.back, child: CommonView.getBackBtnView()),
          Obx(() {
            return Opacity(
              opacity: controller.opacity,
              child: Text(StringName.newsTitle,
                  style: TextStyle(
                      fontSize: 16.sp,
                      color: ColorName.black90,
                      fontWeight: FontWeight.bold)),
            );
          }),
          GestureDetector(
              onTap: controller.newsClick,
              child:
              Platform.isIOS ? Assets.images.iconFriendNews.image(width: 24.w, height: 24.w) : Assets.images.iconFriendNewsAndroid.image(width: 24.w, height: 24.w))
        ],
      ),
    );
  }

  SliverToBoxAdapter buildSliverWaiting() {
    return SliverToBoxAdapter(child: Obx(() {
      if (controller.waitingNewsCount == 0) {
        return SizedBox.shrink();
      }
      return GestureDetector(
        onTap: () => controller.onMessageWaitingClick(),
        child: buildMessageItem(Assets.images.iconNewsItem.provider(),
            title: StringName.newsRequestTitle,
            content: StringName.newsRequestDesc,
            contentTextStyle:
                TextStyle(fontSize: 12.sp, color: ColorName.black60),
            createTime: controller.latestFriendRequest?.createTime ?? 0,
            statusWidget: Container(
              height: 16.w,
              constraints: BoxConstraints(
                minWidth: 16.w,
              ),
              padding: EdgeInsets.symmetric(horizontal: 2.w),
              decoration: BoxDecoration(
                color: '#FF333D'.color,
                borderRadius: BorderRadius.circular(100),
              ),
              child: Center(
                child: Text('${controller.waitingNewsCount}',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontSize: 10.sp, color: ColorName.white, height: 1)),
              ),
            )),
      );
    }));
  }

  Widget buildNewsItem(BuildContext context, int index) {
    final item = controller.messageList[index];
    return GestureDetector(
      onLongPress: () {
        if (item.type == 3 || item.type == 4) {
          controller.onUserIsLongEventClick(index);
        }
      },
      child: buildMessageInfoItem(
          item, (info) => controller.onMessageFunClick(info),
          isShowReportButton: Platform.isIOS && (item.type == 3),
          onReportEvent: (item.type == 3) ? controller.onReportEventClick : null
      ),
    );
  }

  Widget buildNoMessageView() {
    return Align(
      alignment: Alignment(0, -0.1),
      child: IntrinsicHeight(
        child: Column(
          children: [
            Assets.images.iconNews.image(width: 150.w),
            SizedBox(height: 9.w),
            Text(StringName.messageNoData,
                style: TextStyle(fontSize: 16.sp, color: ColorName.black60))
          ],
        ),
      ),
    );
  }
}
