/// 注册模板
library;

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:loopin/IM/im_core.dart' as im_core;
import 'package:loopin/IM/im_service.dart';
import 'package:loopin/api/common_api.dart';
import 'package:loopin/components/my_toast.dart';
import 'package:loopin/components/web_page.dart';
import 'package:loopin/controller/video_module_controller.dart';
import 'package:loopin/service/http.dart';
import 'package:loopin/styles/index.dart';
import 'package:loopin/utils/storage.dart';
import 'package:shirne_dialog/shirne_dialog.dart';

import '../../utils/index.dart';

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

  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
  final Map authObj = {'phonenumber': '', 'smsCode': '', 'clientId': '428a8310cd442757ae699df5d894f051', 'grantType': 'sms'};
  bool isChecked = false;
  final fieldController = TextEditingController();
  Timer? timer;
  String vcodeText = '获取验证码';
  bool disabled = false;
  int time = 60;

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

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

  // 清空文本框
  void handleClear() {
    fieldController.clear();
    setState(() {
      authObj['phonenumber'] = '';
    });
  }

  // 提交表单
  void handleSubmit() async {
    FocusScope.of(context).unfocus(); // 收起键盘
    if (!isChecked) {
      MyToast().tip(title: '未勾选隐私协议');
      return;
    }
    if (authObj['phonenumber'] == '') {
      MyDialog.toast('手机号不能为空', icon: Icon(Icons.warning), style: ToastStyle(backgroundColor: Colors.red.withAlpha(200)));
    } else if (!Utils.checkTel(authObj['phonenumber'])) {
      MyDialog.toast('手机号格式不正确', icon: Icon(Icons.warning), style: ToastStyle(backgroundColor: Colors.red.withAlpha(200)));
    } else if (authObj['smsCode'] == '') {
      MyDialog.toast('验证码不能为空', icon: Icon(Icons.warning), style: ToastStyle(backgroundColor: Colors.red.withAlpha(200)));
    } else {
      final dialogController = MyDialog.loading('正在登录...');

      // 执行登录
      logger.i(authObj);
      final res = await Http.post(CommonApi.login, data: authObj);
      final obj = res['data'];
      // 拿到userid和usersing进行im登录
      logger.i(res);
      String userId = obj['userId'];
      String userSig = obj['userSig'];
      // 初始化im_sdk
      final initRes = await im_core.ImCore.init(sdkAppId: 1600080789);
      if (initRes) {
        // String userId = '1940667704585248769'; //13212279365
        // String userId = '1943510443312078850'; //18832510385
        // String userSig =
        // 'eJwtjcEKgkAURf9l1iFPm*e8EdoYYUWFURAtg5nk5VRiEln0703q8p57Ofcj9qtd8LS1SEQUgBh1mY29NXzmDodaQhwrBRIJI0kq1sPsYcpTVbERSRgDAIEi3Tf2VXFtPUfEyFc9bfj6ZwrH4J1Ig4UL-6LX0ihyS7U5bi-Wzd8LzrK8TFs6TJ1sZwWGxlGas71PxPcHwH4y9Q__';
        // 'eJwtzLEKwjAUheF3ySwlNzXNbcHFxSIOaqTWUUgsF1FDG2tEfHdj2-F8P5wPO2x00tuWFUwknM2GTcbePV1oYEBMhQSeopxyZ65n58iwAjLOOXKF*VhscNTa6FJKEdOonm5-UxJQpZhN2lET3599Xllbv9ZBH2uHuDfvst5tG6FX0EFYVhpOpZ973z8W7PsDmYwyIw__';

        try {
          final loginRes = await ImService.instance.login(userID: userId, userSig: userSig);

          if (loginRes.success) {
            // 存储登录信息
            Storage.write('hasLogged', true);
            Storage.write('userSig', userSig);
            Storage.write('userId', userId);
            Storage.write('token', obj['access_token']);
            // 获取用户账户信息
            // final accountRes = await Http.get(CommonApi.accountInfo);
            // logger.e(accountRes);
            // 刷新短视频列表
            final videoController = Get.find<VideoModuleController>();
            videoController.markNeedRefresh();
            dialogController.close();

            Get.back();
          }
        } catch (e) {
          logger.i(e);
        }
      } else {
        logger.e('登陆异常：im初始化失败');
      }
    }
  }

  // 60s倒计时
  void handleVcode() {
    logger.i(authObj);
    FocusScope.of(context).unfocus(); // 收起键盘

    if (authObj['phonenumber'] == '') {
      MyDialog.toast('手机号不能为空', icon: Icon(Icons.warning), style: ToastStyle(backgroundColor: Colors.red.withAlpha(200)));
    } else if (!Utils.checkTel(authObj['phonenumber'])) {
      MyDialog.toast('手机号格式不正确', icon: Icon(Icons.warning), style: ToastStyle(backgroundColor: Colors.red.withAlpha(200)));
    } else {
      setState(() {
        disabled = true;
      });
      startTimer();
      getCode();
    }
  }

  startTimer() {
    timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      setState(() {
        if (time > 0) {
          vcodeText = '获取验证码(${time--})';
        } else {
          vcodeText = '获取验证码';
          time = 60;
          disabled = false;
          timer.cancel();
        }
      });
    });
  }

  void getCode() async {
    final res = await Http.get(CommonApi.getCode, params: {'phonenumber': authObj['phonenumber']});
    logger.i(res);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () => FocusScope.of(context).unfocus(),
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          forceMaterialTransparency: true,
          toolbarHeight: 0,
        ),
        body: SingleChildScrollView(
          child: Container(
            alignment: Alignment.center,
            margin: const EdgeInsets.only(top: 50.0),
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 30.0),
                  child: Column(
                    children: [
                      ClipOval(
                        child: Image.asset(
                          'assets/images/logo/logo.png',
                          height: 75.0,
                          width: 75.0,
                          fit: BoxFit.cover,
                        ),
                      ),
                      const SizedBox(
                        height: 5.0,
                      ),
                      const Text('无终街',
                          style: TextStyle(
                            color: Color(0xFFFF5000),
                            fontSize: 28.0,
                            fontFamily: 'Maven Pro',
                          )),
                    ],
                  ),
                ),
                Container(
                  height: 40.0,
                  margin: const EdgeInsets.symmetric(vertical: 5.0, horizontal: 30.0),
                  decoration: BoxDecoration(
                    color: Color(0xFFFAF8F5),
                    borderRadius: BorderRadius.circular(15.0),
                  ),
                  child: Row(
                    children: [
                      Expanded(
                        child: TextField(
                          keyboardType: TextInputType.phone,
                          controller: fieldController,
                          inputFormatters: [
                            LengthLimitingTextInputFormatter(11), // 限制输入长度为 11
                            FilteringTextInputFormatter.digitsOnly, // 限制只能输入数字
                          ],
                          decoration: InputDecoration(
                            hintText: '输入手机号',
                            hintStyle: const TextStyle(color: Colors.black38),
                            suffixIcon: Visibility(
                                visible: authObj['phonenumber'].isNotEmpty,
                                child: InkWell(
                                  hoverColor: Colors.transparent,
                                  highlightColor: Colors.transparent,
                                  splashColor: Colors.transparent,
                                  onTap: handleClear,
                                  child: const Icon(
                                    Icons.clear,
                                    color: Colors.grey,
                                    size: 16.0,
                                  ),
                                )),
                            contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 12.0),
                            border: const OutlineInputBorder(borderSide: BorderSide.none),
                          ),
                          onChanged: (value) {
                            setState(() {
                              authObj['phonenumber'] = value;
                            });
                          },
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  height: 40.0,
                  margin: const EdgeInsets.symmetric(vertical: 5.0, horizontal: 30.0),
                  decoration: BoxDecoration(color: Color(0xFFFAF8F5), borderRadius: BorderRadius.circular(15.0)),
                  child: Row(
                    children: [
                      Expanded(
                        child: TextField(
                          keyboardType: TextInputType.phone,
                          inputFormatters: [
                            LengthLimitingTextInputFormatter(6),
                            FilteringTextInputFormatter.digitsOnly, // 限制只能输入数字
                          ],
                          decoration: const InputDecoration(
                            hintText: '验证码',
                            hintStyle: TextStyle(color: Colors.black38),
                            contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 12.0),
                            border: OutlineInputBorder(borderSide: BorderSide.none),
                          ),
                          onChanged: (value) {
                            setState(() {
                              authObj['smsCode'] = value;
                            });
                          },
                        ),
                      ),
                      SizedBox(
                        height: 25.0,
                        child: Container(
                          margin: const EdgeInsets.only(right: 8.0),
                          child: ElevatedButton(
                            style: ButtonStyle(
                                backgroundColor: WidgetStateProperty.all(Colors.white),
                                shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.0))),
                                padding: WidgetStateProperty.all(EdgeInsets.symmetric(horizontal: 15.0))),
                            onPressed: !disabled ? handleVcode : null,
                            child: Text(vcodeText, style: const TextStyle(fontSize: 13.0)),
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                    margin: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 30.0),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(15.0),
                        // 自定义按钮渐变色
                        gradient: const LinearGradient(
                          begin: Alignment.topLeft,
                          end: Alignment.bottomRight,
                          colors: [Color(0xFFFF389B), Color(0xFFFFBA31)],
                        )),
                    child: SizedBox(
                      width: double.infinity,
                      height: 45.0,
                      child: FilledButton(
                        style: ButtonStyle(
                            backgroundColor: WidgetStateProperty.all(Colors.transparent),
                            shadowColor: WidgetStateProperty.all(Colors.transparent),
                            shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)))),
                        onPressed: handleSubmit,
                        child: const Text(
                          '登录',
                          style: TextStyle(fontSize: 16.0),
                        ),
                      ),
                    )),
                Padding(
                  padding: const EdgeInsets.fromLTRB(24.0, 0.0, 24.0, 0.0),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.end, // 顶部对齐
                    children: [
                      Transform.scale(
                        scale: 0.8, // 调整 Checkbox 大小，使和文字高度接近
                        child: Checkbox(
                          value: isChecked,
                          onChanged: (value) {
                            setState(() {
                              isChecked = value ?? false;
                            });
                          },
                          shape: const CircleBorder(),
                          materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                          activeColor: FStyle.primaryColor,
                        ),
                      ),
                      Expanded(
                        child: GestureDetector(
                          onTap: () {
                            logger.w(3555);
                            FocusScope.of(context).unfocus();
                            Get.to(
                              () => SafeArea(
                                child: WebPage(url: 'https://www.wuzhongjie.com.cn/download/yinsizhengce.html', title: '隐私协议'),
                              ),
                            );
                          },
                          child: Text.rich(
                            TextSpan(
                              text: "未注册的手机号验证后将自动创建用户账号，登录即代表您已同意",
                              style: const TextStyle(
                                fontSize: 12,
                                color: Colors.grey,
                                height: 1.1,
                              ),
                              children: [
                                TextSpan(
                                  text: "《使用条款及隐私协议》",
                                  style: const TextStyle(
                                    fontSize: 12,
                                    color: FStyle.primaryColor,
                                    height: 1.2,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
