import 'package:bip39_multi_language/bip39.dart' as bip39;
import 'package:chat/configs/app_colors.dart';
import 'package:chat/routes/auth_routes.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

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

  @override
  State<AuthCreatePage> createState() => _AuthCreatePageState();
}

class _AuthCreatePageState extends State<AuthCreatePage>
    with SingleTickerProviderStateMixin {
  late final TabController _tabController;

  List<String> _englishMnemonic = [];
  List<String> _chineseMnemonic = [];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(
      length: 2,
      vsync: this,
    );
    _generateMnemonic();
  }

  void _generateMnemonic() {
    String _englishBip39Str = bip39.generateMnemonic(language: 'english');
    String _chineseBip39Str = bip39.generateMnemonic(language: 'chinese');

    setState(() {
      _englishMnemonic = _englishBip39Str.split(' ');
      _chineseMnemonic = _chineseBip39Str.split(' ');
    });
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          title: const Text('备份助记词'),
          actions: [
            TextButton(
              onPressed: () {},
              child: const Text('跳过备份'),
            ),
          ],
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                TabBar(
                  controller: _tabController,
                  isScrollable: true,
                  indicatorColor: AppColors.primary,
                  indicatorWeight: 5,
                  indicatorSize: TabBarIndicatorSize.label,
                  tabs: const [
                    Tab(
                      text: 'English',
                    ),
                    Tab(
                      text: '中文',
                    ),
                  ],
                ),
                const Padding(
                  padding: EdgeInsets.only(right: 8.0),
                  child: Text(
                    '请务必抄下助记词，确定之后将进行校验',
                    style: TextStyle(
                      fontSize: 12,
                    ),
                  ),
                ),
              ],
            ),
            Expanded(
              child: TabBarView(
                controller: _tabController,
                children: [
                  _moArea(_englishMnemonic),
                  _moArea(_chineseMnemonic),
                ],
              ),
            ),
            Container(
              height: Get.height * 0.382,
              padding: const EdgeInsets.all(16),
              child: const Text(
                '提示：请勿截图！如果有人获取您的助记词将直接获取您的资产，请抄写助记词并存放在安全的地方，我们会在下一屏幕进行校验，',
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                ElevatedButton(
                  onPressed: () {
                    _generateMnemonic();
                  },
                  child: const Text('更换助记词'),
                ),
                ElevatedButton(
                  onPressed: () {
                    var language = _tabController.index;
                    Get.toNamed(
                      AuthRoutes.createVerify,
                      arguments: {
                        'language': language == 0 ? 'english' : 'chinese',
                        'mnemonic':
                            language == 0 ? _englishMnemonic : _chineseMnemonic
                      },
                    );
                  },
                  child: const Text('开始备份'),
                ),
              ],
            ),
            const SizedBox(
              height: 16,
            ),
          ],
        ),
      ),
    );
  }

  Widget _moArea(List<String> mnemonic) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: GridView.builder(
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          childAspectRatio: 4.5 / 1,
          crossAxisSpacing: 8,
          mainAxisSpacing: 8,
        ),
        itemCount: mnemonic.length,
        itemBuilder: (_, i) {
          return Container(
            padding: const EdgeInsets.all(8),
            decoration: BoxDecoration(
              border: Border.all(
                color: AppColors.border,
                width: 1,
              ),
              borderRadius: BorderRadius.circular(8),
            ),
            child: Text(
              '${i + 1}. ${mnemonic[i]}',
              style: const TextStyle(
                fontSize: 16,
              ),
            ),
          );
        },
      ),
    );
  }
}
