import 'package:chat/configs/app_colors.dart';
import 'package:chat/routes/app_routes.dart';
import 'package:chat/services/auth_service.dart';
import 'package:chat/utils/hd_wallet.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

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

  @override
  State<AuthImportPage> createState() => _AuthImportPageState();
}

class _AuthImportPageState extends State<AuthImportPage> {
  final TextEditingController _editingController = TextEditingController();

  @override
  void initState() {
    super.initState();

    setState(() {
      _editingController.text =
          'demise sell awesome tragic faith village party elbow lady leopard wrestle civil';
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('导入账户'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            TextField(
              controller: _editingController,
              maxLines: 5,
              decoration: const InputDecoration(
                hintText: '请输入您的助记词，以空格分割单词或汉字',
                border: OutlineInputBorder(
                  borderSide: BorderSide(
                    color: AppColors.border,
                    width: 0.4,
                  ),
                ),
              ),
            ),
            const SizedBox(height: 16),
            const Text('支持导入所有遵循BIP标准生成的助记词'),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: () async {
                String? address =
                    HDWallet.mnemonicToAddress(_editingController.text);
                if (address != null) {
                  var result = await AuthService.to.login(address);
                  if (result) {
                    Get.offAllNamed(AppRoutes.app);
                  }
                }
              },
              child: const Text('开始导入'),
            ),
          ],
        ),
      ),
    );
  }
}
