import 'package:flutter/Material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:keyboard/base/base_view.dart';
import 'package:lottie/lottie.dart';

import '../../../resource/assets.gen.dart';
import '../../../resource/colors.gen.dart';
import '../../../resource/string.gen.dart';
import '../../../utils/status_bar_util.dart';
import '../../../widget/app_lifecycle_widget.dart';
import '../../../widget/gradient_btn.dart';
import '../../../widget/status_bar_placeholder_widget.dart';
import '../../../widget/top_bar.dart';
import '../widget/tutorial_header_widget.dart';
import 'keyboard_tutorial_android_view_controller.dart';

/// 键盘教程页-Android平台
class KeyboardTutorialAndroidView
    extends BaseView<KeyboardTutorialAndroidViewController> {
  KeyboardTutorialAndroidView({
    super.key,
    required bool checkTutorialComplete,
  }) {
    // 更新值
    controller.setCheckTutorialComplete(checkTutorialComplete);
  }

  @override
  Color backgroundColor() {
    return Color(0xFFF6F5FA);
  }

  @override
  Widget buildBody(BuildContext context) {
    return AppLifecycleWidget(
      onAppLifecycleCallback: (isForeground) async {
        if (isForeground) {
          // 未完成，则每次切换到前台时，重新检查设置，更新按钮状态
          controller.checkSetting();
          // 步骤都做完了，则跳转去键盘引导页
          if (controller.checkTutorialComplete.value) {
            if (await controller.checkHasComplete()) {
              return;
            }
          }
        }
      },
      child: Stack(
        children: [
          // 头部
          Positioned(left: 0, top: 0, right: 0, child: _buildHeader(context)),
          // 状态栏和标题栏
          Positioned(
            left: 0,
            top: 0,
            right: 0,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [StatusBarPlaceholderWidget(), _buildTopBar()],
            ),
          ),
          // 内容，填充剩余部分
          Positioned.fill(
            top: 170.h + StatusBarUtil.getStatusBarHeight(Get.context!),
            left: 0,
            right: 0,
            child: Column(children: [_buildContent()]),
          ),
          // 底部按钮
          Positioned(
            left: 0,
            right: 0,
            bottom: 0,
            child: _buildBottomActionBtn(),
          ),
        ],
      ),
    );
  }

  /// 顶部栏
  Widget _buildTopBar() {
    return TopBar(
      // 返回按钮
      leftWidget: GestureDetector(
        onTap: controller.clickBack,
        child: Assets.images.iconBlackBackArrow.image(
          width: 24.w,
          height: 24.h,
        ),
      ),
      // 视频教程按钮
      rightWidget: GestureDetector(
        onTap: () {
          controller.clickTutorialVideo();
        },
        child: Text(
          StringName.keyboardTutorialVideo,
          style: TextStyle(
            color: ColorName.black85,
            fontSize: 14.sp,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
    );
  }

  /// 头部布局
  Widget _buildHeader(BuildContext context) {
    return TutorialHeaderWidget();
  }

  /// 内容
  Widget _buildContent() {
    return Expanded(child: Column(children: [_buildTutorialAnimation()]));
  }

  /// 键盘设置引导动画
  Widget _buildTutorialAnimation() {
    return Lottie.asset(
      Assets.anim.animKeyboardTutorialSettingAndroid,
      repeat: true,
      width: 340.w,
    );
  }

  /// 启用键盘按钮
  Widget _buildEnableKeyboard() {
    return Obx(() {
      return Container(
        margin: EdgeInsets.symmetric(horizontal: 50.w),
        child: GradientTextBtn(
          // 是否启用，这里UI设计上，未启用时要高亮按钮，所以要取反
          enable: !controller.isKeyboardEnable.value,
          StringName.tutorialEnableKeyboardStep,
          onPressed: () {
            if (controller.checkTutorialComplete.value) {
              // 已启用，不响应点击
              if (controller.isKeyboardEnable.value) {
                return;
              }
            }
            // 去启用键盘
            controller.goEnableKeyboard();
          },
        ),
      );
    });
  }

  /// 悬浮球设置按钮
  Widget _buildFloatingBallActionBtn() {
    return Obx(() {
      bool isKeyboardEnable = controller.isKeyboardEnable.value;
      bool isFloatingWindowEnable = controller.isFloatingWindowEnable.value;
      // 启用了键盘，但悬浮窗权限未获取到，则高亮按钮
      bool isHighlightBtn = isKeyboardEnable && !isFloatingWindowEnable;
      return Container(
        margin: EdgeInsets.symmetric(horizontal: 50.w),
        child: GradientTextBtn(
          enable: isHighlightBtn,
          StringName.tutorialEnableFloatingBallStep,
          onPressed: () {
            if (controller.checkTutorialComplete.value) {
              // 已获取，不响应点击
              if (controller.isFloatingWindowEnable.value) {
                return;
              }
            }
            // 去启用悬浮球
            controller.jumpFloatingWindowSetting();
          },
        ),
      );
    });
  }

  /// 底部操作按钮
  Widget _buildBottomActionBtn() {
    return Column(
      children: [
        // 启用键盘
        _buildEnableKeyboard(),
        SizedBox(height: 20.h),
        // 启用悬浮球
        _buildFloatingBallActionBtn(),
        // 距离底部一定距离
        SizedBox(height: 25.h),
      ],
    );
  }
}
