import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_wallet/base/YSBase.dart';
import 'package:flutter_wallet/generated/l10n.dart';
import 'package:flutter_wallet/tools/YSColors.dart';
import 'package:flutter_wallet/tools/YSTools.dart';
import 'package:web3dart/credentials.dart';

class YSWalletExport extends StatefulWidget {
  final Map wallet;
  const YSWalletExport({Key? key, required this.wallet}) : super(key: key);

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

class YSWalletExportState extends State<YSWalletExport> with SingleTickerProviderStateMixin{
  final List _titleArray = [
    {'title':S.current.KEYSTOREWENJIAN,'type':1},
    {'title':S.current.ERWEIMA,'type':2}
  ];
  late TabController _tabController;
  String _store = '';
  @override
  void initState() {
    _getStore();
    _tabController = TabController(
      vsync: this,
      length: _titleArray.length,
    );
    super.initState();
  }

  _getStore() {
    Wallet wallet = Wallet.createNew(EthPrivateKey.fromHex(widget.wallet['private']), widget.wallet['password'], Random(100));
    _store = wallet.toJson();
  }
  @override
  Widget build(BuildContext context) {
    LogUtil.d(widget.wallet);
    return YSBase(
      isBake: true,
      ysChild: Padding(
        padding: EdgeInsets.only(left: hsp(20),right: hsp(20)),
        child: DefaultTabController(
          length: _titleArray.length,
          child: Column(
            children: [
              SizedBox(
                height: hsp(45),
                child: TabBar(
                  controller: _tabController,
                  indicatorWeight: hsp(1),
                  labelColor: Colors.blue,
                  tabs: _titleArray.map((f) {
                    return Tab(
                      child: Text(f['title']),
                    );
                  }).toList(),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: hsp(20)),
                height: ysHeight(context)-ysTOP(context)-hsp(150),
                child: TabBarView(
                  controller: _tabController,
                  children: _titleArray.map((f) {
                    return Align(
                      alignment: Alignment.center,
                      child: f['type']==1?Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          RichText(text: TextSpan(
                            style: TextStyle(fontSize: zsp(20),color: Colors.blue),
                            children: [
                               TextSpan(
                                text: '${S.current.LIXIANBAOCUN}\n'
                              ),
                              TextSpan(
                                text: S.current.QINGFUZHIZHANTIE,
                                style: TextStyle(fontSize: zsp(14),color: Colors.grey)
                              ),
                              TextSpan(
                                  text: '\n\n${S.current.QINGWUSHIYONGWANGLUO}\n'
                              ),
                              TextSpan(
                                  text: S.current.QINGWUTONGGYOWANGLUOGONGJU,
                                  style: TextStyle(fontSize: zsp(14),color: Colors.grey)
                              ),
                              TextSpan(
                                  text: '\n\n${S.current.MIMABAOXIANXIANGBAOCUN}\n'
                              ),
                              TextSpan(
                                  text: '${S.current.RUXUZAIXIANBAOCUN}\n',
                                  style: TextStyle(fontSize: zsp(14),color: Colors.grey)
                              )
                            ]
                          )),
                          Container(
                            height: hsp(200),
                            decoration: BoxDecoration(
                                color: YSColors.lineColor(context),
                                borderRadius: const BorderRadius.all(Radius.circular(10))
                            ),
                            padding: EdgeInsets.all(hsp(15)),
                            child: Text(_store,style: TextStyle(fontSize: zsp(14),color: Colors.grey),),
                          ),
                          GestureDetector(
                            onTap: (){
                              YSCopyPaste.copy(_store);
                            },
                            behavior: HitTestBehavior.opaque,
                            child: Container(
                              margin: EdgeInsets.only(top: hsp(50)),
                              height: hsp(50),
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                  color: YSColors.buttonColor(context),
                                  borderRadius: const BorderRadius.all(Radius.circular(5))
                              ),
                              child: Text(S.current.FUZHI,style: YSColors.buttonStyle(context),),
                            ),
                          )

                        ],
                      ):Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          RichText(text: TextSpan(
                              style: TextStyle(fontSize: zsp(20),color: Colors.blue),
                              children: [
                                TextSpan(
                                    text: '${S.current.JINGONGZHIJIESAOMIAO}\n'
                                ),
                                TextSpan(
                                    text: S.current.ERWEIMAJINZHIBAOCUN,
                                    style: TextStyle(fontSize: zsp(14),color: Colors.grey)
                                ),
                                TextSpan(
                                    text: '\n\n${S.current.ZAIANQUANHUANJING}\n'
                                ),
                                TextSpan(
                                    text: '${S.current.ZAIANQUANHUANJINGSHIYONGQINGZAIQUEBAO}\n',
                                    style: TextStyle(fontSize: zsp(14),color: Colors.grey)
                                )
                              ]
                          )),
                          Align(
                            alignment: Alignment.center,
                            child: SizedBox(
                              height: hsp(200),
                              width: hsp(200),
                              child: YSCodeImage.codeImage(_store),
                            ),
                          )
                        ],
                      ),
                    );
                  }).toList(),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
