import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:wgshare/routes/app_routes.dart';
import 'package:wgshare/utils/cus_behavior.dart';
import 'package:wgshare/utils/toast_utils.dart';
import '../../utils/color_util.dart';
import '../../view/upgrade/loadJson/load_network_json.dart';
import 'home_logic.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => HomePageState();
}

class HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {
  final logic = Get.put(HomeLogic());
  final state = Get.find<HomeLogic>().state;

  Timer? _timer;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return PopScope(
      canPop: false,
        onPopInvoked: (bool didPop) async {
          if (didPop) {
            return;
          }
          if (logic.closeOnConfirm(context)) {
            // 系统级别导航栈 退出程序
            SystemNavigator.pop();
          }
        },
      child: Scaffold(
          appBar: AppBar(
            surfaceTintColor: Colors.white,
            elevation: 0,
            toolbarHeight: 0,
            systemOverlayStyle: const SystemUiOverlayStyle(
              statusBarColor: Colors.transparent,
              systemNavigationBarColor: Color(0xFF000000),
              systemNavigationBarIconBrightness: Brightness.light,
              statusBarIconBrightness: Brightness.dark,
              statusBarBrightness: Brightness.light,
            ),
            backgroundColor: Colors.white,
          ),
          body: Obx(() => Column(
            children: [
              Container(
                width: double.infinity,
                height: 44.h,
                alignment: Alignment.center,
                color: Colors.white,
                child: Text(
                  '会议列表',
                  style: TextStyle(
                      fontSize: 16.sp,
                      color: ColorUtil.Color_51_51_51,
                      fontWeight: FontWeight.w500),
                ),
              ),
              Expanded(
                child: Container(
                  color: ColorUtil.Color_244_244_244,
                  child: SmartRefresher(
                    enablePullUp: true,
                    controller: state.refreshController,
                    onRefresh: logic.onRefresh,
                    onLoading: logic.onLoading,
                    child: ListView.builder(
                      itemBuilder: (context, index) {
                        return Container(
                          width: double.infinity,
                          decoration: const BoxDecoration(
                            borderRadius:
                            BorderRadius.all(Radius.circular(6)),
                            color: Colors.white,
                          ),
                          margin: EdgeInsets.only(
                              top: index == 0 ? 20 : 12,
                              bottom: index == 19 ? 20 : 0,
                              left: 16,
                              right: 16),
                          padding: const EdgeInsets.all(12),
                          child: Column(
                            children: [
                              Row(
                                mainAxisAlignment:
                                MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    state.meetingRooms.value[index].roomName,
                                    style: TextStyle(
                                        fontSize: 14.sp,
                                        color: ColorUtil.Color_89_88_88,
                                        fontWeight: FontWeight.w500),
                                  ),
                                  Row(
                                    children: [
                                      Image.asset(
                                        'assets/images/index_persons.png',
                                        width: 16.w,
                                        height: 16.h,
                                      ),
                                      Text(
                                        '${state.meetingRooms.value[index].onlineUserCount}人',
                                        style: TextStyle(
                                          fontSize: 12.sp,
                                          color: ColorUtil.Color_177_177_177,
                                        ),
                                      ),
                                    ],
                                  )
                                ],
                              ),
                              SizedBox(height: 20.h),
                              Row(
                                mainAxisAlignment:
                                MainAxisAlignment.spaceBetween,
                                children: [
                                  Row(
                                    children: [
                                      Text(
                                        state.meetingRooms.value[index]
                                            .roomNum,
                                        style: TextStyle(
                                          fontSize: 12.sp,
                                          color: ColorUtil.Color_177_177_177,
                                        ),
                                      ),
                                      SizedBox(width: 6.w),
                                      GestureDetector(
                                        child: Image.asset(
                                          'assets/images/index_copy.png',
                                          width: 16.w,
                                          height: 16.h,
                                        ),
                                        onTap: () {
                                          Clipboard.setData(ClipboardData(
                                              text: state.meetingRooms
                                                  .value[index].roomNum));
                                          ToastUtils.showSuccess("复制成功");
                                        },
                                      )
                                    ],
                                  ),
                                  GestureDetector(
                                    child: Container(
                                      width: 78.w,
                                      height: 30.h,
                                      decoration: BoxDecoration(
                                        borderRadius: const BorderRadius.all(
                                            Radius.circular(6)),
                                        color: ColorUtil.Color_85_117_242,
                                      ),
                                      child: Row(
                                        mainAxisAlignment:
                                        MainAxisAlignment.center,
                                        children: [
                                          Text(
                                            '进入',
                                            style: TextStyle(
                                              fontSize: 12.sp,
                                              color: Colors.white,
                                            ),
                                          ),
                                          Image.asset(
                                            'assets/images/index_right.png',
                                            width: 16.w,
                                            height: 16.h,
                                          )
                                        ],
                                      ),
                                    ),
                                    onTap: () {
                                      logic.gotoMeetingRoom(context, index);
                                    },
                                  )
                                ],
                              )
                            ],
                          ),
                        );
                      },
                      itemCount: state.meetingRooms.value.length,
                    ),
                  ),
                ),
              )
            ],
          ))),
    );
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }
}
