import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:smart_hotel_app/blocs/auth/auth_bloc.dart';
import 'package:smart_hotel_app/blocs/auth/auth_event.dart';
import 'package:smart_hotel_app/views/profile/index/cubit/profile_cubit.dart';
import 'package:smart_hotel_app/blocs/auth/auth_state.dart';
import 'package:smart_hotel_app/views/profile/index/widget/profile_header_block.dart';
import 'package:smart_hotel_app/views/profile/index/widget/profile_menu_block.dart';

import 'cubit/profile_state.dart';

@RoutePage()
class ProfileView extends StatelessWidget {
  const ProfileView({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) {
        final authState = context.read<AuthBloc>().state;
        final userInfo = authState is AuthSuccess ? authState.userInfo : null;
        return ProfileCubit(userInfo: userInfo);
      },
      child: _ProfileContent(),
    );
  }
}

class _ProfileContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<ProfileCubit, ProfileState>(
      builder: (context, state) {
        return LayoutBuilder(
          builder: (context, constraints) {
            return SingleChildScrollView(
              child: ConstrainedBox(
                constraints: BoxConstraints(
                  minHeight: constraints.maxHeight,
                ),
                child: Container(
                  width: double.infinity,
                  color: const Color.fromRGBO(242, 243, 245, 1),
                  child: Column(
                    children: [
                      ProfileHeaderBlock(
                        username: state.username,
                        welcomeText: state.welcomeText,
                      ),
                      SizedBox(height: 16.h),
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: 28.w),
                        child: ProfileMenuBlock(
                          menuList: state.menuList,
                          onLogout: () {
                            _showHandleDialog(context);
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            );
          },
        );
      },
    );
  }

  void _showHandleDialog(BuildContext context) {
    showDialog(
      context: context,
      barrierDismissible: true,
      builder: (dialogContext) {
        return Dialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(24.r),
          ),
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 28.w, vertical: 28.h),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(24.r),
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  '温馨提示',
                  style: TextStyle(
                    fontSize: 34.sp,
                    fontWeight: FontWeight.bold,
                    color: Colors.black,
                  ),
                ),
                SizedBox(height: 30.h),
                Text(
                  '确定退出登录吗？',
                  style: TextStyle(
                    fontSize: 32.sp,
                    color: Colors.black,
                  ),
                ),
                SizedBox(height: 50.h),
                Row(
                  spacing: 28.w,
                  children: [
                    Expanded(
                        child: GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {
                            Navigator.of(dialogContext).pop();
                          },
                          child: Container(
                            height: 80.h,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              color: const Color.fromRGBO(229, 241, 255, 1),
                              borderRadius: BorderRadius.circular(16.r),
                            ),
                            child: Text(
                              '取消',
                              style: TextStyle(
                                fontSize: 32.sp,
                                color: const Color.fromRGBO(100, 116, 139, 1),
                              ),
                            ),
                          ),
                        )
                    ),
                    Expanded(
                        child: GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {
                            Navigator.of(dialogContext).pop();
                            context.read<AuthBloc>().add(const AuthLogoutRequestedEvent());
                          },
                          child: Container(
                            height: 80.h,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              color: const Color.fromRGBO(73, 149, 234, 1),
                              borderRadius: BorderRadius.circular(16.r),
                            ),
                            child: Text(
                              '确定',
                              style: TextStyle(
                                fontSize: 32.sp,
                                color: Colors.white,
                              ),
                            ),
                          ),
                        )
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }

}
