import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:odf/tools/login/login_model.dart';

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

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  late StreamSubscription subscription;
  final LoginModel _viewmodel = LoginModel();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    subscription = _viewmodel.streamController.stream.listen((event) {
      String code = event['code'];
      if (code.isNotEmpty) {
        switch (code) {
          case "appLogin":
            Navigator.of(context).pushReplacementNamed("/HomePage");
            break;

          case "error":
            EasyLoading.showToast(event['data']);
            break;
        }
      }
    });
  }

  String userName = "";
  String passWord = "";

  void _userNameChanged(String str) {
    userName = str;
    setState(() {});
  }

  void _passWordChanged(String str) {
    passWord = str;
    setState(() {});
  }

  @override
  void dispose() {
    // TODO: implement dispose
    subscription.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final t100 = size.width / 3.6;
    final s20 = size.width / 18;
    final t50 = size.width / 7.2;
    final l10 = size.width / 36;
    final s14 = size.width / 25.714285714285;
    final t20 = size.width / 18;
    final t80 = size.width / 4.5;
    final s18 = size.width / 20;

    return Scaffold(
      backgroundColor: const Color(0xF8F8F8FF),
      resizeToAvoidBottomInset: false,
      body: Container(
        decoration: const BoxDecoration(
            image: DecorationImage(
          image: AssetImage('assets/images/login_bg.png'),
          fit: BoxFit.cover,
        )),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Container(
              margin: EdgeInsets.only(top: t100),
              child: Text(
                "光缆资源管理系统",
                style: TextStyle(fontSize: s20, fontWeight: FontWeight.w600),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: t50),
              alignment: Alignment.center,
              child: Container(
                height: t50,
                alignment: Alignment.center,
                margin: EdgeInsets.only(left: t50, right: t50),
                padding: EdgeInsets.only(left: l10),
                decoration: BoxDecoration(
                  color: const Color(0xFFECEFF3),
                  border: Border.all(width: 0.5, color: Colors.black12),
                  borderRadius: BorderRadius.all(Radius.circular(l10)),
                ),
                child: TextField(
                  cursorColor: const Color(0xFF1A73EC),
                  onChanged: _userNameChanged,
                  decoration: const InputDecoration(
                    hintText: '请输入账号',
                    border: InputBorder.none,
                    enabledBorder: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    hintStyle: TextStyle(color: Color(0xFF999999)),
                  ),
                  style: TextStyle(fontSize: s14),
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: t20),
              alignment: Alignment.center,
              child: Container(
                height: t50,
                alignment: Alignment.center,
                margin: EdgeInsets.only(left: t50, right: t50),
                padding: EdgeInsets.only(left: l10),
                decoration: BoxDecoration(
                  color: const Color(0xFFECEFF3),
                  border: Border.all(width: 0.5, color: Colors.black12),
                  borderRadius: BorderRadius.all(Radius.circular(l10)),
                ),
                child: TextField(
                  cursorColor: const Color(0xFF1A73EC),
                  keyboardType: TextInputType.visiblePassword,
                  obscureText: true,
                  onChanged: _passWordChanged,
                  decoration: const InputDecoration(
                    hintText: '请输入密码',
                    border: InputBorder.none,
                    enabledBorder: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    hintStyle: TextStyle(color: Color(0xFF999999)),
                  ),
                  style: TextStyle(fontSize: s14),
                ),
              ),
            ),
            GestureDetector(
              onTap: () {
                _viewmodel.appLogin(userName, passWord);
              },
              child: Container(
                height: t50,
                margin: EdgeInsets.only(top: t80, left: t50, right: t50),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: const Color(0xFF1A73EC),
                  borderRadius: BorderRadius.all(Radius.circular(l10)),
                ),
                child: Text(
                  "登录",
                  style: TextStyle(fontSize: s18, color: Colors.white),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
