import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:fis_theme/theme.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flyinsono/pages/auth/clause_view/state.dart';
import 'package:flyinsono/pages/auth/clause_view/view.dart';
import 'package:flyinsono/views/common/device_live/widgets/clause_dialog.dart';
import 'package:flyinsono/views/common/device_live/widgets/clause_page.dart';
import 'package:get/get.dart';

///条款界面勾选行
class ClauseCheckRow extends FStatefulWidget implements FInteractiveContainer {
  ClauseCheckRow({
    this.initalValue = false,
    required this.onChanged,
  });

  /// 初始化值
  final bool initalValue;

  /// 改变条款值的回调
  final ValueChanged<bool> onChanged;
  @override
  FState<ClauseCheckRow> createState() => _ClauseCheckRowState();

  @override
  String get pageName => 'ClauseCheckRow';
}

///条款界面勾选行状态
class _ClauseCheckRowState extends FState<ClauseCheckRow> {
  @override
  FWidget build(BuildContext context) {
    final linkColor = FTheme.ins.colorScheme.primary;
    final content = FRichText(
      text: TextSpan(
        style: TextStyle(
          fontFamily: FTheme.ins.localeSetting.fontFamily,
          color: Colors.grey,
          fontSize: 14.sp,
          height: 1.4,
        ),
        children: [
          TextSpan(text: i18nBook.auth.agreeStatement.t + " "),
          TextSpan(
            text: i18nBook.auth.userAgreement.t,
            style: TextStyle(
              color: linkColor,
              fontFamily: FTheme.ins.localeSetting.fontFamily,
            ),
            recognizer: TapGestureRecognizer()
              ..onTap = () async {
                if (kIsWeb)
                  Get.dialog(
                    ClauseDialog(title: i18nBook.auth.userAgreement.t),
                    arguments: ClauseDocType.userAgreement,
                  );
                else {
                  await Get.to(
                    () => ClausePage(title: i18nBook.auth.userAgreement.t),
                    arguments: ClauseDocType.userAgreement,
                    transition: Transition.rightToLeft,
                    duration: ClauseViewPage.C_TRANSITION_DURATION,
                  );
                }
              },
          ),
          TextSpan(
            text: " " + i18nBook.common.and.t + " ",
          ),
          TextSpan(
            text: i18nBook.auth.privacyPolicy.t,
            style: TextStyle(color: linkColor),
            recognizer: TapGestureRecognizer()
              ..onTap = () async {
                if (kIsWeb)
                  Get.dialog(
                    ClauseDialog(title: i18nBook.auth.privacyPolicy.t),
                    arguments: ClauseDocType.privacyPolicy,
                  );
                else {
                  await Get.to(
                    () => ClausePage(title: i18nBook.auth.privacyPolicy.t),
                    arguments: ClauseDocType.privacyPolicy,
                    transition: Transition.rightToLeft,
                    duration: ClauseViewPage.C_TRANSITION_DURATION,
                  );
                }
              },
          ),
        ],
      ),
      textDirection: TextDirection.ltr,
    );
    return FFlex(
      direction: Axis.horizontal,
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FSizedBox(
          width: 28.sp,
          height: 22.sp,
          child: FMaterialCheckbox(
            shape: RoundedRectangleBorder(
              side: BorderSide.none,
              borderRadius: BorderRadius.circular(
                4.sp,
              ),
            ),
            checkColor: Colors.white,
            activeColor: FTheme.ins.colorScheme.primary,
            value: widget.initalValue,
            onChanged: (val) => widget.onChanged.call(
              val == true,
            ),
          ),
        ),
        FFlexible(
          child: content,
        ),
      ],
    );
  }
}
