import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/pages/components/select.dart';
import 'package:flyinsono/pages/user/user_center/controller.dart';
import 'package:get/get.dart';

import 'index.dart';
import 'types.dart';
import 'widget/roleCard.dart';

///身份验证
class RoleAuthenticationPage extends FisView<RoleAuthenticationController>
    implements FWidget {
  RoleAuthenticationPage({Key? key}) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return context.fResponsiveVoid(
      mobile: () => _MobileLayout(),
      desktop: () => _DesktopLayout(),
    );
  }
}

class _DesktopLayout extends FisView<RoleAuthenticationController>
    implements FWidget {
  final ScrollController scrollController = ScrollController();
  late final userCenterController = Get.find<UserCenterController>();

  late final roles = userCenterController.state.roles;
  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        i18nBook.user.selectIdentityAuth.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      backgroundColor: Colors.white,
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      onClose: onclose,
      contentPadding: EdgeInsets.all(0),
      insetPadding: EdgeInsets.symmetric(vertical: 40),
      isUseColumnLayout: true,
      children: [
        Expanded(
          child: Container(
            width: 600,
            child: ListView(
              children: roles.map((role) {
                return Container(
                  margin:
                      EdgeInsets.symmetric(vertical: 20.sp, horizontal: 40.sp),
                  child: RoleCard(
                    startColor: role.startColor,
                    endColor: role.endColor,
                    icon: FImage.network(role.iconUrl), //角色图标
                    roleType: role.roleName,
                    roleCode: role.roleCode,
                    descList: role.descriptionLines,
                  ),
                );
              }).toList(),
            ),
          ),
        )
      ],
    );
  }

  ///关闭
  void onclose() {
    final userCenterController = Get.find<UserCenterController>();
    if (userCenterController.state.applyState == ApplyStateEnum.NotApply) {
      router.back();
    } else {
      router.back(id: NavIds.HOME);
    }
  }
}

class _MobileLayout extends FisView<RoleAuthenticationController>
    implements FWidget {
  final userCenterController = Get.find<UserCenterController>();
  @override
  FWidget build(BuildContext context) {
    return FScaffold(
      backgroundColor: Colors.white,
      appBar: FAppBar.simple(
        title: i18nBook.user.identityToAuth.t,
        onBackClicked: () => router.back(),
      ),
      body: FSafeArea(
        child: _buildList(),
      ),
    );
  }

  FWidget _buildList() {
    final separator = FSizedBox(height: 5.sp);
    final dataList = userCenterController.state.roles;
    return FListView.separated(
      padding: EdgeInsets.only(
        top: 10.sp,
        bottom: 25.sp,
      ),
      itemBuilder: (_, index) => _createMobileCard(dataList[index]),
      separatorBuilder: (_, index) => separator,
      itemCount: dataList.length,
    );
  }

  ///角色卡片
  FWidget _createMobileCard(UserRole data) {
    return FContainer(
      margin: EdgeInsets.symmetric(horizontal: 10.sp, vertical: 5.sp),
      child: RoleCard(
        startColor: data.startColor,
        endColor: data.endColor,
        icon: FImage.network(data.iconUrl), //角色图标
        roleType: data.roleName,
        roleCode: data.roleCode,
        descList: data.descriptionLines,
      ),
    );
  }
}
