import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/utils/window__change_helpler.dart';
import 'package:flyinsono/global.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/color/lab_dark_colors.dart';
import 'package:flyinsono/lab/components/clock/large_clock.dart';
import 'package:flyinsono/pages/others/title_bar_buttons.dart';
import 'package:get/get.dart';
import 'package:fis_common/env/env.dart';
import 'index.dart';
import 'widgets/text_input_field.dart';

class LabLoginPage extends GetView<LabLoginController> {
  const LabLoginPage({Key? key}) : super(key: key);

  get tabHashCode => this.hashCode.toString();

  // 主视图
  Widget _buildView() {
    return Stack(
      children: [
        _buildBackgroundImage(),
        Center(
          child: Container(
            width: 900,
            height: 500,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8).copyWith(
                topRight: Radius.zero,
                bottomRight: Radius.zero,
              ),
            ),
            child: Row(
              children: [_buildRightShow(), _buildLeftFrom()],
            ),
          ),
        ),
        //if (kDebugMode)
        if (FPlatform.isWindows && !Global.isVStationSlave)
          TitleBarButtons(
            WindowChangeHelper.onMinimize,
            WindowChangeHelper.onClose,
            WindowChangeHelper.onMaximize,
            LabColors.base100,
            Colors.transparent,
            hoverColor: LabColorConfig.base500,
            isLabHeader: true,
            isShowNotification: false,
          ),
      ],
    );
  }

  Widget _buildRightShow() {
    return Expanded(
      flex: 6,
      child: Stack(
        children: [
          _buildLabImage(),
          // Positioned(
          //   top: 180,
          //   left: 50,
          //   child: Column(
          //     crossAxisAlignment: CrossAxisAlignment.start,
          //     children: [
          //       Text(
          //         "智能超声 一触即达",
          //         style: TextStyle(
          //           fontSize: 26,
          //           color: LabColors.text800,
          //         ),
          //       ),
          //       Text(
          //         "More than Ultrasound",
          //         style: TextStyle(
          //           fontSize: 28,
          //           color: LabColors.text800,
          //         ),
          //       ),
          //     ],
          //   ),
          // ),
          Positioned(
            left: 28,
            bottom: 40,
            child: Container(
              height: 120,
              padding: EdgeInsets.symmetric(horizontal: 30, vertical: 30),
              color: Colors.transparent,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  LargeCustomClock(),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildLeftFrom() {
    return Expanded(
      flex: 5,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          SizedBox(height: 40),
          _buildLogo(),
          SizedBox(height: 40),
          _buildLoginFields(),
          // SizedBox(height: 30),
          Container(
            constraints: BoxConstraints(maxWidth: 340, minWidth: 20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                IconButton(
                  onPressed: () async {
                    await controller.onNetworkSettingClick();
                  },
                  icon: Icon(
                    Icons.settings_outlined,
                    color: LabColors.base500,
                  ),
                ),
                if (FPlatform.isWindows || kDebugMode) _buildLoginViaUSBKey(),
              ],
            ),
          )
        ],
      ),
    );
  }

  Widget _buildBackgroundImage() {
    return SizedBox.expand(
      child: Image.asset(
        "assets/images/lab_login.png",
        fit: BoxFit.cover,
      ),
    );
  }

  Widget _buildLabImage() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(8).copyWith(
        topRight: Radius.zero,
        bottomRight: Radius.zero,
      ),
      child: SizedBox.expand(
        child: Image.asset(
          "assets/images/lab_image.png",
          fit: BoxFit.cover,
        ),
      ),
    );
  }

  Widget _buildLogo() {
    return Column(
      children: [
        Container(
          height: 60,
          child: ColorFiltered(
            colorFilter: ColorFilter.mode(LabColors.base500, BlendMode.srcIn),
            child: Image.asset(
              Global.isVStationSlave
                  ? "assets/images/logo_icon_0.png"
                  : "assets/images/logo_icon.png",
              filterQuality: FilterQuality.medium,
              isAntiAlias: true,
              fit: BoxFit.contain,
            ),
          ),
        ),
        Text(
          "FLYINSONO LAB",
          style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.w900,
            color: LabColors.base500,
          ),
        ),
      ],
    );
  }

  Widget _buildLoginViaUSBKey() {
    return Row(
      // crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        SizedBox(
          width: 80,
        ),
        Icon(
          Icons.key,
          color: LabColors.base500,
        ),
        TextButton(
          onPressed: () {
            controller.dongleLoginAsync();
          },
          child: Text(
            '使用智能卡登录',
            style: TextStyle(
              decoration: TextDecoration.underline,
            ),
          ),
          style: ButtonStyle(
            foregroundColor:
                MaterialStateProperty.all<Color>(LabColors.base500),
          ),
        ),
      ],
    );
  }

  Widget _buildLoginFields() {
    return Container(
      constraints: BoxConstraints(maxWidth: 380, maxHeight: 140),
      child: Row(
        children: [
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              // crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                TextInputField(
                  hintText: "用户名/手机号/邮箱",
                  controller: TextEditingController(
                    text: controller.state.username,
                  ),
                  onChanged: (p0) {
                    controller.state.username = p0;
                  },
                ),
                SizedBox(
                  height: 10,
                ),
                TextInputField(
                  hintText: "密码",
                  password: true,
                  controller: TextEditingController(
                    text: controller.state.password,
                  ),
                  onChanged: (p0) {
                    controller.state.password = p0;
                  },
                  onSubmitted: (p0) {
                    controller.commonLoginAsync();
                  },
                ),
                Container(
                  margin: EdgeInsets.only(top: 10),
                  constraints: BoxConstraints(maxWidth: 320, minWidth: 20),
                  height: 40,
                  decoration: BoxDecoration(
                    color: LabColors.base600,
                    borderRadius: BorderRadius.circular(4),
                  ),
                  // padding: EdgeInsets.symmetric(horizontal: 10),
                  child: Material(
                    color: LabColors.base600,
                    borderRadius: BorderRadius.circular(4),
                    child: InkWell(
                      child: Row(
                        children: [
                          Expanded(
                            child: Icon(
                              Icons.arrow_forward,
                              color: LabColors.base900,
                            ),
                          ),
                        ],
                      ),
                      // color: LabColors.base600,
                      onTap: () {
                        controller.commonLoginAsync();
                      },
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildVersion() {
    return Container(
      alignment: Alignment.bottomRight,
      child: Text(
        // TODO 从配置文件中读取版本号
        "Version 1.0.0-beta",
        style: TextStyle(
          fontSize: 12,
          color: LabColors.text400,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabLoginController>(
      init: LabLoginController(),
      id: LabLoginController.labLoginId,
      builder: (_) {
        return _buildView();
      },
    );
  }
}
