import 'package:electronic_assistant/base/base_page.dart';
import 'package:electronic_assistant/data/consts/Constants.dart';
import 'package:electronic_assistant/data/repositories/account_repository.dart';
import 'package:electronic_assistant/module/browser/view.dart';
import 'package:electronic_assistant/resource/assets.gen.dart';
import 'package:electronic_assistant/resource/colors.gen.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:electronic_assistant/widget/login_code_btn.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../router/app_pages.dart';
import 'controller.dart';

enum LoginFromType { talkDetail, mainLogin, store, aiChat }

class LoginPage extends BasePage<LoginController> {
  const LoginPage({super.key});

  static Future<bool> start({LoginFromType? fromType}) async {
    final result =
        await Get.toNamed(RoutePath.login, arguments: {"fromType": fromType});
    return result == true;
  }

  @override
  bool immersive() {
    return true;
  }

  // 点击空白处收起键盘
  @override
  void backgroundOnTapEvent() {
    super.backgroundOnTapEvent();
    FocusScope.of(Get.context!).requestFocus(FocusNode());
  }

  @override
  Widget buildBody(BuildContext context) {
    return Stack(
      children: [
        _buildBackgroundImage(),
        Scaffold(
          resizeToAvoidBottomInset: false,
          backgroundColor: Colors.transparent,
          appBar: AppBar(
            leading: IconButton(
              icon: const Icon(Icons.arrow_back_ios_new_rounded),
              onPressed: () {
                Get.back(result: accountRepository.isLogin.value);
              },
            ),
            backgroundColor: Colors.transparent,
            systemOverlayStyle: SystemUiOverlayStyle.dark,
          ),
          body: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                margin: EdgeInsets.only(left: 28.w, top: 64.h),
                width: 194.w,
                height: 71.h,
                child: Image(image: Assets.images.iconLoginLogo.provider()),
              ),
              _buildPhoneTextField(),
              _buildCodeTextField(),
              _buildLoginBtn(),
              _buildAgreeText(),
            ],
          ),
        ),
      ],
    );
  }

  // 背景图片
  Widget _buildBackgroundImage() {
    return Image(image: Assets.images.bgLogin.provider());
  }

  // 电话号码输入框
  Widget _buildPhoneTextField() {
    return Container(
      margin: EdgeInsets.only(top: 44.h, left: 28.w, right: 28.w),
      alignment: Alignment.center,
      height: 44.h,
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            width: 1.0,
            color: "#F0F0F0".toColor(),
          ),
        ),
      ),
      child: TextField(
        focusNode: controller.phoneFocusNode,
        maxLines: 1,
        maxLength: 11,
        cursorColor: ColorName.colorPrimary,
        keyboardType: TextInputType.phone,
        textAlignVertical: TextAlignVertical.center,
        textInputAction: TextInputAction.next,
        decoration: InputDecoration(
          hintText: '输入手机号码',
          counterText: '',
          hintStyle: TextStyle(fontSize: 16, color: "#AFAFAF".toColor()),
          labelStyle: const TextStyle(
            fontSize: 16,
            color: ColorName.primaryTextColor,
          ),
          contentPadding: const EdgeInsets.all(0),
          border: const OutlineInputBorder(borderSide: BorderSide.none),
          enabled: true,
        ),
        style: TextStyle(fontSize: 14.sp),
        onChanged: (value) {
          controller.setPhone(value);
        },
      ),
    );
  }

  // 验证码输入框
  Widget _buildCodeTextField() {
    return Container(
      margin: EdgeInsets.only(top: 36.h, left: 28.w, right: 28.w),
      alignment: Alignment.center,
      height: 44.h,
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            width: 1.0,
            color: "#F0F0F0".toColor(),
          ),
        ),
      ),
      child: Row(
        children: [
          Expanded(
            child: TextField(
              maxLines: 1,
              maxLength: 4,
              keyboardType: TextInputType.number,
              inputFormatters: [FilteringTextInputFormatter.digitsOnly],
              cursorColor: ColorName.colorPrimary,
              textAlignVertical: TextAlignVertical.center,
              textInputAction: TextInputAction.done,
              decoration: InputDecoration(
                counterText: '',
                hintText: '输入验证码',
                hintStyle: TextStyle(fontSize: 16, color: "#AFAFAF".toColor()),
                labelStyle: const TextStyle(
                  fontSize: 16,
                  color: ColorName.primaryTextColor,
                ),
                contentPadding: const EdgeInsets.all(0),
                border: const OutlineInputBorder(borderSide: BorderSide.none),
                enabled: true,
              ),
              style: TextStyle(fontSize: 14.sp),
              onChanged: (value) {
                controller.setCode(value);
              },
            ),
          ),
          LoginCodeBtn(
            onTapCallback: () {
              controller.getUserCode();
            },
            available: true,
          ),
        ],
      ),
    );
  }

  // 登录按钮
  Widget _buildLoginBtn() {
    return GestureDetector(
      onTap: () {
        controller.login();
      },
      child: Container(
        margin: EdgeInsets.only(
          top: 54.h,
          left: 28.w,
          right: 28.w,
        ),
        height: 48.h,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors: ['#6177F2'.toColor(), '#8B9DFF'.toColor()],
            stops: const [0, 1.0],
          ),
          borderRadius: BorderRadius.circular(8),
        ),
        child: const Text(
          "登录",
          style: TextStyle(
            color: ColorName.white,
            fontSize: 16,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
    );
  }

  Widget _buildAgreeText() {
    return Container(
      margin: EdgeInsets.only(left: 28.w, top: 16.h),
      child: Row(
        children: [
          Obx(
            () {
              return GestureDetector(
                onTap: () {
                  controller.isAgree.value = !controller.isAgree.value;
                },
                child: SizedBox(
                    width: 20.w,
                    height: 20.w,
                    child: controller.isAgree.value
                        ? Assets.images.iconSelectTrue.image()
                        : Assets.images.iconSelectFalse.image()),
              );
            },
          ),
          Text(
            "我已阅读并同意",
            style: TextStyle(
              color: "#AFAFAF".toColor(),
              fontSize: 12,
            ),
          ),
          GestureDetector(
            onTap: () {
              if (GetPlatform.isIOS) {
                BrowserPage.start(Constants.privacyPolicyIos);
              } else {
                BrowserPage.start(Constants.privacyPolicy);
              }
            },
            child: Text(
              "《隐私政策》",
              style: TextStyle(
                color: "#5E8BFF".toColor(),
                fontSize: 12,
              ),
            ),
          ),
          Text(
            "和",
            style: TextStyle(
              color: "#AFAFAF".toColor(),
              fontSize: 12,
            ),
          ),
          GestureDetector(
            onTap: () {
              BrowserPage.start(Constants.userAgreement);
            },
            child: Text(
              "《用户使用协议》",
              style: TextStyle(
                color: "#5E8BFF".toColor(),
                fontSize: 12,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
