import 'package:chat/configs/app_colors.dart';
import 'package:chat/services/auth_service.dart';
import 'package:chat/views/home/widgets/pop_menu_item.dart';
import 'package:chat/views/public/scan_page.dart';
import 'package:chat/widgets/custom_avatar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:qr_flutter/qr_flutter.dart';

class UserQrCodePage extends StatefulWidget {
  const UserQrCodePage({Key? key}) : super(key: key);

  @override
  State<UserQrCodePage> createState() => _UserQrCodePageState();
}

class _UserQrCodePageState extends State<UserQrCodePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('我的二维码'),
        actions: [
          IconButton(
            onPressed: () {
              _showLongPressMenu();
            },
            icon: const Icon(Icons.more_horiz),
          ),
        ],
      ),
      body: Center(
        child: Container(
          width: Get.width - 32,
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: AppColors.white,
            borderRadius: BorderRadius.circular(8),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                children: [
                  CustomAvatar(
                    AuthService.to.userInfo.value.avatar,
                    size: 64,
                    radius: 6,
                  ),
                  const SizedBox(width: 16),
                  Text(
                    AuthService.to.userInfo.value.nickname ?? '',
                    style: const TextStyle(
                      fontSize: 24,
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 32),
              QrImage(
                data: 'BEFRIEND|${AuthService.to.userInfo.value.username}',
                version: 3,
                size: Get.width * 0.8,
              ),
              const SizedBox(height: 32),
              const Text(
                '扫一扫上面的二维码，加我ZH-CHAT好友',
                style: TextStyle(
                  color: AppColors.unactive,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> _showLongPressMenu() async {
    showModalBottomSheet(
      context: Get.context!,
      isScrollControlled: true,
      backgroundColor: AppColors.white,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(8)),
      ),
      builder: (context) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            PopMenuItem(
              '保存到手机',
              onTap: () {},
            ),
            const Divider(height: 0),
            PopMenuItem(
              '扫描二维码',
              onTap: () {
                Get.back();
                Permission.camera.request().isGranted.then((value) {
                  if (value) {
                    Get.to(const ScanPage());
                  }
                });
              },
            ),
            const Divider(height: 0),
            const Divider(height: 0.4),
            Container(
              color: AppColors.page,
              height: 8,
            ),
            const Divider(height: 0.4),
            PopMenuItem(
              '取消',
              onTap: () {
                Get.back();
              },
            ),
          ],
        );
      },
    );
  }
}
