import 'dart:io';

import 'package:EasyDartModule/EasyDartModule.dart' as edm;
import 'package:ef/ef.dart';
import 'package:flutter/material.dart';
import 'package:flutterflow_ui/flutterflow_ui.dart';
import 'package:vbvs_app/common/color/appConstants.dart';
import 'package:vbvs_app/common/color/app_uri_status.dart';
import 'package:vbvs_app/common/util/DailyLogUtils.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/component/tool/ClickableContainer.dart';
import 'package:vbvs_app/component/tool/CustomCard.dart';
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
import 'package:vbvs_app/controller/device/blueteeth_bind_controller.dart';
import 'package:vbvs_app/controller/main_bottom/global_controller.dart';
import 'package:vbvs_app/controller/person/person_controller.dart';
import 'package:vbvs_app/controller/theme_controller/ThemeController.dart';
import 'package:vbvs_app/controller/user_info_controller.dart';
import 'package:vbvs_app/model/api_response.dart';

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

  @override
  State<UpdateUserPage> createState() => _UpdateUserPageState();
}

class _UpdateUserPageState extends State<UpdateUserPage> {
  GlobalController globalController = Get.find();
  UserInfoController userInfoController = Get.find();
  BlueteethBindController blueteethBindController = Get.find();
  PersonController personController = Get.find();
  ThemeController themeController = Get.find();

  @override
  void initState() {
    super.initState();
    userInfoController.model.user!.tmpHead = null;
    userInfoController.model.user!.tmpNickName = null;
    userInfoController.model.user!.tmpHead =
        userInfoController.model.user!.avatar;
    userInfoController.model.user!.tmpNickName =
        userInfoController.model.user!.nick_name;
  }

  @override
  Widget build(BuildContext context) {
    int login = userInfoController.model.login!;
    return LayoutBuilder(
      builder: (context, bodySize) => GestureDetector(
        onTap: () => FocusScope.of(context).unfocus(),
        child: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/img/bgNoImg.png'),
              fit: BoxFit.fill,
            ),
          ),
          child: Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
              backgroundColor: themeController.currentColor.sc17,
              automaticallyImplyLeading: false,
              iconTheme: IconThemeData(
                color: themeController.currentColor.sc3,
              ),
              titleSpacing: 0,
              title: Container(
                width: double.infinity,
                height: 180.rpx,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    Text(
                      '修改资料页.个人信息'.tr,
                      style: TextStyle(
                        fontFamily: 'Readex Pro',
                        color: themeController.currentColor.sc3,
                        letterSpacing: 0,
                        fontSize: 30.rpx,
                      ),
                    ),
                    Positioned(
                      left: 0,
                      child: returnIconButtom,
                    ),
                    Positioned(
                      right: 20.rpx,
                      child: CustomCard(
                        borderRadius: 20.rpx,
                        onTap: () async {
                          ApiResponse apiResponse =
                              await userInfoController.updateData();
                          TopSlideNotification.show(
                            context,
                            text: apiResponse.msg!,
                            textColor: apiResponse.code == HttpStatusCodes.ok
                                ? themeController.currentColor.sc2
                                : themeController.currentColor.sc9,
                          );
                          if (apiResponse.code == HttpStatusCodes.ok) {
                            userInfoController.model.user!.tmpHead = null;
                            userInfoController.model.user!.tmpNickName = null;
                            await userInfoController.getUserInfo();
                            userInfoController.updateAll();
                            Get.back();
                          }
                        },
                        colors: [
                          themeController.currentColor.sc1,
                          themeController.currentColor.sc2,
                        ],
                        child: Container(
                          width: 100.rpx,
                          height: 60.rpx,
                          alignment: Alignment.center,
                          padding: EdgeInsetsDirectional.fromSTEB(
                              16.rpx, 0.rpx, 16.rpx, 0.rpx),
                          child: Text(
                            '修改资料页.保存'.tr,
                            style: TextStyle(
                              fontFamily: 'Inter Tight',
                              color: themeController.currentColor.sc3,
                              letterSpacing: 0.0,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              actions: [],
              centerTitle: false,
            ),
            body: SafeArea(
              top: true,
              child: Container(
                width: double.infinity,
                height: double.infinity,
                decoration: BoxDecoration(),
                child: Padding(
                  padding: EdgeInsetsDirectional.fromSTEB(
                      60.rpx, 0.rpx, 60.rpx, 0.rpx),
                  child: Column(
                    mainAxisSize: MainAxisSize.max,
                    children: [
                      Padding(
                        padding: EdgeInsetsDirectional.fromSTEB(
                            0.rpx, 76.rpx, 0.rpx, 0.rpx),
                        child: Container(
                            width: MediaQuery.sizeOf(context).width * 0.213,
                            height: MediaQuery.sizeOf(context).height * 0.098,
                            constraints: BoxConstraints(
                              minWidth: 160.rpx,
                              minHeight: 160.rpx,
                            ),
                            decoration: BoxDecoration(),
                            child: Obx(() {
                              return getImageWidget(context);
                            })),
                      ),
                      ClickableContainer(
                        backgroundColor: Colors.transparent,
                        highlightColor: themeController.currentColor.sc2,
                        padding: EdgeInsetsDirectional.fromSTEB(
                            0.rpx, 44.rpx, 0.rpx, 44.rpx),
                        borderRadius: 0,
                        onTap: () async {
                          edm.EasyDartModule.logger
                              .info("${userInfoController.model.user!}点击上传头像");
                          DailyLogUtils.writeLog(
                              "${userInfoController.model.user!}点击上传头像");
                          ApiResponse apiResponse =
                              await userInfoController.uploadImg();
                          TopSlideNotification.show(
                            context,
                            text: apiResponse.msg!,
                            textColor: apiResponse.code == HttpStatusCodes.ok
                                ? themeController.currentColor.sc2
                                : themeController.currentColor.sc9,
                          );
                        },
                        child: Center(
                          child: Text(
                            '修改资料页.点击更换头像'.tr,
                            style: TextStyle(
                              fontFamily: 'Inter',
                              fontSize: 26.rpx,
                              letterSpacing: 0.0,
                              color: themeController.currentColor.sc2,
                            ),
                          ),
                        ),
                      ),
                      Padding(
                        padding: EdgeInsetsDirectional.fromSTEB(
                            0.rpx, 79.rpx, 0.rpx, 0.rpx),
                        child: Container(
                          width: double.infinity,
                          decoration: BoxDecoration(
                            border: Border(
                              top: BorderSide(
                                color: themeController.currentColor.sc4
                                    .withOpacity(0.5),
                                width: AppConstants().border_width,
                              ),
                              bottom: BorderSide(
                                color: themeController.currentColor.sc4
                                    .withOpacity(0.5),
                                width: AppConstants().border_width,
                              ),
                            ),
                          ),
                          child: Row(
                            mainAxisSize: MainAxisSize.max,
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Expanded(
                                child: Row(
                                  mainAxisSize: MainAxisSize.max,
                                  children: [
                                    Expanded(
                                      child: Container(
                                        width: 100.rpx,
                                        height: 100.rpx,
                                        decoration: BoxDecoration(),
                                        child: Align(
                                          alignment: AlignmentDirectional(0, 0),
                                          child: TextFormField(
                                            initialValue: userInfoController
                                                .model.user!.nick_name,
                                            onChanged: (value) {
                                              userInfoController.model.user!
                                                  .tmpNickName = value;
                                            },
                                            autofocus: false,
                                            obscureText: false,
                                            decoration: InputDecoration(
                                              isDense: true,
                                              labelStyle: TextStyle(
                                                fontFamily: 'Inter',
                                                fontSize: 26.rpx,
                                                letterSpacing: 0.0,
                                              ),
                                              hintText: '未命名'.tr,
                                              hintStyle: TextStyle(
                                                fontFamily: 'Inter',
                                                fontSize: 26.rpx,
                                                letterSpacing: 0.0,
                                                color: themeController
                                                    .currentColor.sc4,
                                              ),
                                              enabledBorder: OutlineInputBorder(
                                                borderSide: BorderSide(
                                                  color: Color(0x00000000),
                                                  width: 1.rpx,
                                                ),
                                                borderRadius:
                                                    BorderRadius.circular(
                                                        8.rpx),
                                              ),
                                              focusedBorder: OutlineInputBorder(
                                                borderSide: BorderSide(
                                                  color: Color(0x00000000),
                                                  width: 1.rpx,
                                                ),
                                                borderRadius:
                                                    BorderRadius.circular(
                                                        8.rpx),
                                              ),
                                              errorBorder: OutlineInputBorder(
                                                borderSide: BorderSide(
                                                  width: 1.rpx,
                                                ),
                                                borderRadius:
                                                    BorderRadius.circular(
                                                        8.rpx),
                                              ),
                                              focusedErrorBorder:
                                                  OutlineInputBorder(
                                                borderSide: BorderSide(
                                                  width: 1.rpx,
                                                ),
                                                borderRadius:
                                                    BorderRadius.circular(
                                                        8.rpx),
                                              ),
                                              filled: false,
                                            ),
                                            style: TextStyle(
                                                  fontFamily: 'Inter',
                                                  fontSize: 26.rpx,
                                                  letterSpacing: 0.0,
                                                  color: themeController
                                                      .currentColor.sc3,
                                                ),
                                            textAlign: TextAlign.center,
                                            cursorColor: themeController
                                                .currentColor.sc3,
                                          ),
                                        ),
                                      ),
                                    ),
                                  ].divide(SizedBox(width: 27.rpx)),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget getImageWidget(BuildContext context) {
    try {
      UserInfoController controller = Get.find();
      var head = controller.model.user!.tmpHead;
      return Container(
        width: 200.rpx,
        height: 200.rpx,
        child: Container(
          clipBehavior: Clip.antiAlias,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
          ),
          child: head == null || head.isEmpty
              ? Image.asset(
                  'assets/img/avatar.png',
                  fit: BoxFit.cover,
                )
              : Image.network(
                  head,
                  fit: BoxFit.cover,
                ),
        ),
      );
    } catch (e) {
      print(e);
    }
    return Container();
  }
}
