/*
 * @Author: wangyang 1147192855@qq.com
 * @Date: 2022-07-05 16:42:48
 * @LastEditors: wangyang 1147192855@qq.com
 * @LastEditTime: 2022-07-29 11:56:18
 * @FilePath: \marking_app\lib\pages\mine\index.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:cached_network_image/cached_network_image.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:marking_app/common/mixin/common.dart';
import 'package:marking_app/common/model/enum/subject.dart';
import 'package:marking_app/provider/do_marking_provider.dart';
import 'package:marking_app/provider/user_provider.dart';
import 'package:marking_app/routes/RouterManager.dart';
import 'package:marking_app/utils/fluro/index.dart';
import 'package:marking_app/utils/index.dart';
import 'package:marking_app/utils/my_text.dart';

class TheMine extends StatefulHookConsumerWidget {
  const TheMine({Key? key}) : super(key: key);

  @override
  _TheMineState createState() => _TheMineState();
}

class _TheMineState extends ConsumerState<TheMine> with CommonMixin {
  @override
  Widget build(BuildContext context) {
    final userState = ref.watch(userProvider);
    final tokenState = ref.watch(userTokenProvider);

    final personalInfoTitleStly = TextStyle(
      color: const Color.fromRGBO(80, 87, 103, 1),
      fontSize: 16.sp,
    );

    final personalInfoValStly = TextStyle(
      color: const Color.fromRGBO(148, 163, 182, 1),
      fontSize: 16.sp,
    );

    return AnnotatedRegion(
      value: const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        systemNavigationBarIconBrightness: Brightness.light,
        statusBarIconBrightness: Brightness.light,
        statusBarBrightness: Brightness.dark,
      ),
      child: Stack(
        children: [
          SizedBox(
            height: double.infinity,
            child: Column(
              children: [
                Container(
                  height: 240.h,
                  width: double.infinity,
                  decoration: const BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage('assets/images/personal_bgi.png'),
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                Expanded(
                    child: Container(
                  color: const Color.fromRGBO(248, 248, 248, 1),
                ))
              ],
            ),
          ),
          SafeArea(
            child: Scaffold(
              backgroundColor: Colors.transparent,
              body: Column(
                children: [
                  Stack(
                    alignment: const FractionalOffset(0.04, 0.1),
                    children: [
                      Container(
                        height: 200.h,
                        alignment: Alignment.center,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            ClipRRect(
                              borderRadius: BorderRadius.circular(80.w),
                              child: Container(
                                width: 80.w,
                                height: 80.w,
                                padding: EdgeInsets.all(4.w),
                                decoration: BoxDecoration(
                                  border: Border.all(
                                    width: 1.w,
                                    color: Colors.white,
                                  ),
                                  borderRadius: BorderRadius.all(
                                    Radius.circular(40.w),
                                  ),
                                  boxShadow: [
                                    BoxShadow(
                                      color: const Color.fromRGBO(46, 91, 255, 0.2),
                                      offset: Offset(4.w, 6.h), //阴影y轴偏移量
                                      blurRadius: 8, //阴影模糊程度
                                      spreadRadius: 0.2, //阴影扩散程度
                                    )
                                  ],
                                ),
                                child: CachedNetworkImage(
                                  fit: BoxFit.cover,
                                  imageUrl: userState.avatar,
                                  placeholder: (context, url) => Image.asset('assets/images/default_user_dead.png'),
                                  errorWidget: (context, url, error) =>
                                      Image.asset('assets/images/default_user_dead.png'),
                                ),
                              ),
                            ),
                            InkWell(
                              onTap: () {
                                if (tokenState == '' || userState.id == '') {
                                  toLoginPage(context);
                                }
                              },
                              child: Container(
                                margin: EdgeInsets.only(top: 16.h),
                                child: Text(
                                  tokenState != '' ? userState.userName : '请前往登录',
                                  style: TextStyle(fontSize: 16.sp, color: Colors.white),
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      InkWell(
                        onTap: () => Navigator.pop(context),
                        child: Icon(Icons.arrow_back_ios_new_rounded, color: Colors.white, size: 24.sp),
                      ),
                    ],
                  ),
                  SizedBox(height: 14.h),
                  Container(
                    margin: EdgeInsets.symmetric(vertical: 22.h, horizontal: 16.w),
                    padding: EdgeInsets.symmetric(vertical: 22.h, horizontal: 16.w),
                    height: 310.h,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(6.w)),
                      color: Colors.white,
                      boxShadow: const [
                        BoxShadow(
                          color: Color.fromRGBO(46, 91, 255, 0.1),
                          offset: Offset.zero, //阴影y轴偏移量
                          blurRadius: 20, //阴影模糊程度
                          spreadRadius: 10, //阴影扩散程度
                        )
                      ],
                    ),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text('账号', style: personalInfoTitleStly),
                            Text(userState.loginName, style: personalInfoValStly)
                          ],
                        ),
                        Container(
                          height: 1.w,
                          color: const Color.fromRGBO(240, 243, 255, 1),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text('所在学校', style: personalInfoTitleStly),
                            Text(userState.schoolName, style: personalInfoValStly)
                          ],
                        ),
                        Container(
                          height: 1.w,
                          color: const Color.fromRGBO(240, 243, 255, 1),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text('担任职位', style: personalInfoTitleStly),
                            SizedBox(width: 20.w),
                            Expanded(
                              child: Text(
                                userState.positionNames.map((e) => e).toList().join(','),
                                maxLines: 2,
                                textAlign: TextAlign.right,
                                overflow: TextOverflow.ellipsis,
                                style: personalInfoValStly,
                              ),
                            )
                          ],
                        ),
                        Container(height: 1.w, color: const Color.fromRGBO(240, 243, 255, 1)),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text('所授科目', style: personalInfoTitleStly),
                            Expanded(
                              child: Text(
                                userState.subjectIds.map((e) => getSubjectEnumName(e)).toList().join(','),
                                maxLines: 2,
                                textAlign: TextAlign.right,
                                overflow: TextOverflow.ellipsis,
                                style: personalInfoValStly,
                              ),
                            )
                          ],
                        ),
                        Container(height: 1.w, color: const Color.fromRGBO(240, 243, 255, 1)),
                        Padding(
                          padding: EdgeInsets.only(top: 10.h),
                          child: InkWell(
                            onTap: () {
                              RouterManager.router.navigateTo(
                                context,
                                RouterManager.ohterMainPagePath,
                                transition: TransitionType.custom,
                                transitionBuilder: (context, animation, secondaryAnimation, child) {
                                  return SkewTransition(
                                      turns: Tween<double>(
                                        begin: -0.06,
                                        end: 0.0,
                                      ).animate(animation),
                                      child: child);
                                },
                              );
                            },
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text('其他', style: personalInfoTitleStly),
                                Icon(
                                  Icons.arrow_forward_ios,
                                  color: const Color.fromRGBO(80, 87, 103, 1),
                                  size: 16.sp,
                                )
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                  Expanded(
                    child: Column(
                      children: [
                        Expanded(child: SizedBox()),
                        Container(
                          margin: EdgeInsets.only(bottom: 40.h),
                          alignment: Alignment.bottomCenter,
                          child: InkWell(
                            child: Container(
                              padding: EdgeInsets.symmetric(vertical: 14.h),
                              margin: EdgeInsets.only(right: 16.w, left: 16.w),
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.all(
                                  Radius.circular(6.w),
                                ),
                                color: Colors.white,
                                boxShadow: [
                                  BoxShadow(
                                    color: const Color.fromRGBO(46, 91, 255, 0.2),
                                    offset: Offset(2.w, 2.h), //阴影y轴偏移量
                                    blurRadius: 14, //阴影模糊程度
                                    spreadRadius: 0.5, //阴影扩散程度
                                  )
                                ],
                              ),
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Icon(
                                    Icons.exit_to_app_outlined,
                                    size: 16.sp,
                                    color: const Color.fromRGBO(148, 163, 182, 1),
                                  ),
                                  Container(
                                    width: 6.w,
                                  ),
                                  Text(
                                    '退出登录',
                                    style: TextStyle(color: const Color.fromRGBO(148, 163, 182, 1), fontSize: 16.sp),
                                  ),
                                ],
                              ),
                            ),
                            onTap: () {
                              _showAlertDialog(context, ref);
                            },
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 确认对话框
  _showAlertDialog(context1, WidgetRef ref) async {
    await showDialog(
        // 表示点击灰色背景的时候是否消失弹出框
        barrierDismissible: false,
        context: context1,
        builder: (context) {
          return AlertDialog(title: quickText("提示信息"), content: quickText("您确定要退出登录吗?"), actions: <Widget>[
            TextButton(
              child: quickText("取消"),
              onPressed: () {
                Navigator.pop(context, 'Cancle');
              },
            ),
            TextButton(
                child: quickText("确定"),
                onPressed: () {
                  ref.read(markingKeyboardProvider.notifier).clean();
                  ref.read(markingSubtopicSwitchingProvider.notifier).clean();
                  ref.read(userTokenProvider.notifier).clean();
                  ref.read(userProvider.notifier).clean();
                  Navigator.pop(context, "Ok");
                  toLoginPage(context);
                })
          ]);
        });
  }
}
