import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wallet/login/view/YSHelpView.dart';
import 'package:flutter_wallet/tools/YSAlertView.dart';
import 'package:flutter_wallet/tools/YSBip.dart';
import 'package:flutter_wallet/tools/YSSqflite.dart';
import '../base/YSBase.dart';
import '../base/YSTabBar.dart';
import '../generated/l10n.dart';
import '../tools/YSColors.dart';
import '../tools/YSNetWork.dart';
import '../tools/YSTools.dart';

class YSWalletIntoWord extends StatefulWidget {
  final int type;
  const YSWalletIntoWord({Key? key, required this.type}) : super(key: key);

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

class YSWalletIntoWordState extends State<YSWalletIntoWord> {
  String _name = '';
  String _passWord1 = '';
  String _passWord2 = '';
  bool _isFing = true;
  bool _isAgree = false;
  bool _isSec = true;
  final TextEditingController _editingController = TextEditingController();
  @override
  void dispose() {
    _editingController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return YSBase2(
      isScroll: false,
      ysTitle: widget.type==1?S.current.SIYAODAORU:widget.type==2?S.current.ZHUJICIDAORU:S.current.KEYSTOREDAORU,
      ysRightWidget: GestureDetector(
        onTap: () async{
          String result = await YSCodeImage.scanCode();
          _editingController.text = result;
          setState(() {});
        },
        child: Image.asset(YSColors.imageStyle(context, '编组 25'),height: hsp(20),width: hsp(20),),
      ),
      ysChild: Padding(
        padding: EdgeInsets.only(top: hsp(20)),
        child: Column(
          children: [
            Container(
              height: hsp(130),
              width: ysWidth(context)-hsp(80),
              decoration: BoxDecoration(
                  color: YSColors.containColor(context),
                  borderRadius: const BorderRadius.all(Radius.circular(10))
              ),
              child: CupertinoTextField(
                placeholder:
                widget.type==1?S.current.QINGSHURUSIYAO:
                widget.type==2?S.current.QINGSHURUZHUJICIYIKONGGE:
                S.current.QINGSHURUKEYSTORE,
                placeholderStyle: TextStyle(fontSize: zsp(18),color: YSColors.lineColor(context)),
                maxLines: 100,
                textDirection: TextDirection.ltr,
                decoration: const BoxDecoration(),
                padding: EdgeInsets.all(hsp(10)),
                controller: _editingController,
              ),
            ),
            Expanded(
                child: Padding(
                  padding: EdgeInsets.only(top: hsp(20)),
                  child: ClipRRect(
                      borderRadius: const BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10)),
                      child: Container(
                        width: ysWidth(context),
                        padding: EdgeInsets.only(top: hsp(15),bottom: hsp(15),left: 15,right: 15),
                        color: YSColors.backgroundColor(context),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(S.current.SHEZHIQIANBAOMINGCHENG,style: YSColors.contentStyle(context),),
                            YSInputView(tips: S.current.QIANBAOMINGCHENG,valueSetter: (value){
                              _name = value;
                            },),
                            Padding(
                              padding: EdgeInsets.only(top: hsp(15),bottom: hsp(5)),
                              child: Row(
                                children: [
                                  Expanded(child: Text(S.current.SHEZHIQIANBAOMIMA,style: YSColors.contentStyle(context),),),
                                  GestureDetector(
                                    onTap: (){
                                      _isSec = !_isSec;
                                      setState(() {});
                                    },
                                    child: Image.asset(YSColors.imageStyle(context, _isSec?'yanjing_yincang':'眼睛'),height: hsp(20),width: hsp(20),color: YSColors.alertColor(context),),
                                  )
                                ],
                              )
                              ,
                            ),
                            Text('※${S.current.BAKEWALLETBUHUICUNCHUNINDEMIMA}',style: YSColors.subStyle(context),),
                            YSInputView(tips: S.current.ZHISHAOBAWEISHUZIJIDAXIAOXIEZIMU,valueSetter: (value){
                              _passWord1 = value;
                            },isSec: _isSec,),
                            YSInputView(tips: S.current.CHONGFUSHURUYISHANGMIMA,valueSetter: (value){
                              _passWord2 = value;
                            },isSec: _isSec),
                            Padding(
                              padding: EdgeInsets.only(top: hsp(10)),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Expanded(child: Text(S.current.SHIYONGFACEIDZHIWENDENGLU,style: YSColors.contentStyle(context),),),
                                  YSSWitchView(valueSetter: (value){
                                    _isFing = value;
                                  })
                                ],
                              ),
                            ),
                            Padding(
                              padding: EdgeInsets.only(top: hsp(10)),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  YSCheckView(valueSetter: (value){
                                    _isAgree = value;
                                  }),
                                  Expanded(child: Text(' ${S.current.WOLIAOJIEBAKEWALLETWUFAWEIWOZHAOHUIMIMA}',style: YSColors.subStyle(context),),),

                                ],
                              ),
                            )
                          ],
                        ),
                      )
                  ),
                )
            ),
            Container(
              padding: EdgeInsets.only(top: hsp(20),bottom: hsp(20),left: hsp(20),right: hsp(20)),
              color: YSColors.backgroundColor(context),
              child: GestureDetector(
                onTap: (){
                  _postWordData();
                },
                child: Container(
                  height: hsp(50),
                  decoration: BoxDecoration(
                    color: YSColors.buttonColor(context),
                    borderRadius: const BorderRadius.all(Radius.circular(10)),
                  ),
                  alignment: Alignment.center,
                  child: Text(S.current.DAORUQIANBAO,style: YSColors.buttonStyle(context),),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  _intoPage() {
    ysShowCenterAlertView(context, YSAlertMessage(callback: () async{
      if(YSData().isManager){
        List array = await YSSqflite().rawQuery();
        if(!mounted)return;
        YSSqflite.notifier(context, array);
        Navigator.pop(context);
      }else{
        Navigator.of(context).pushAndRemoveUntil(CupertinoPageRoute(builder: (context){
          return const YSTabBar();
        }), (route) => false);
      }

    }),isTrans: true);
  }

  _postWordData() async{
    FocusScope.of(context).unfocus();
    if(widget.type==1){
      if(_editingController.text.isEmpty){
        ysFlutterToast(S.current.QINGSHURUSIYAO);
        return;
      }
    }else if(widget.type==2){
      if(_editingController.text.isEmpty){
        ysFlutterToast(S.current.QINGSHURUZHUJICIYIKONGGE);
        return;
      }
      bool isCheck = YSBipManager.checkMnemonic(_editingController.text);
      if(!isCheck){
        ysFlutterToast('助记词格式错误');
        return;
      }
    }
    if(_name.isEmpty){
      ysFlutterToast(S.current.QIANBAOMINGCHENGBUNENGWEIKONG);
      return;
    }
    bool isPass = isPassword(_passWord1);
    if(!isPass){
      ysFlutterToast(S.current.ZHISHAOBAWEISHUZIJIDAXIAOXIEZIMU);
      return;
    }
    if(_passWord2.isEmpty){
      ysFlutterToast(S.current.CHONGFUSHURUYISHANGMIMA);
      return;
    }
    if(_passWord1!=_passWord2){
      ysFlutterToast(S.current.LIANGCIMIMASHURUBUYIZHI);
      return;
    }
    if(!_isAgree){
      ysFlutterToast(S.current.DIANJIXUANZHONGLIAOJIEMIMAZHAOHUI);
      return;
    }
    YSData().name = _name;
    YSData().password = _passWord1;
    bool isDone = false;
    if(widget.type==1){
      isDone = await YSBipManager.setPrivate(_editingController.text,isInto: true);
    }else if(widget.type==2){
      isDone = await YSBipManager.setMnemonic(_editingController.text,isInto: true);
    }
    if(isDone==true&&mounted){
      _intoPage();
    }
  }
}

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

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

class YSWalletIntoKeystoreState extends State<YSWalletIntoKeystore> {
  String _passWord = '';
  String _name = '';
  bool _isAgree = false;
  final TextEditingController _editingController = TextEditingController();
  @override
  void dispose() {
    _editingController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return YSBase2(
      isScroll: false,
      ysTitle: S.current.KEYSTOREDAORU,
      ysRightWidget: GestureDetector(
        onTap: () async{
          String result = await YSCodeImage.scanCode();
          _editingController.text = result;
          setState(() {});
        },
        child: Image.asset(YSColors.imageStyle(context, '编组 25'),height: hsp(20),width: hsp(20),),
      ),
      ysChild: Padding(
        padding: EdgeInsets.only(top: hsp(20)),
        child: Column(
          children: [
            Container(
              height: hsp(130),
              width: ysWidth(context)-hsp(80),
              decoration: BoxDecoration(
                  color: YSColors.containColor(context),
                  borderRadius: const BorderRadius.all(Radius.circular(10))
              ),
              child: CupertinoTextField(
                placeholder:S.current.QINGSHURUKEYSTORE,
                placeholderStyle: TextStyle(fontSize: zsp(18),color: YSColors.lineColor(context)),
                maxLines: 100,
                textDirection: TextDirection.ltr,
                decoration: const BoxDecoration(),
                padding: EdgeInsets.all(hsp(10)),
                controller: _editingController,
              ),
            ),
            Expanded(
                child: Padding(
                  padding: EdgeInsets.only(top: hsp(20)),
                  child: ClipRRect(
                      borderRadius: const BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10)),
                      child: Container(
                        width: ysWidth(context),
                        padding: EdgeInsets.only(top: hsp(15),bottom: hsp(15),left: 15,right: 15),
                        color: YSColors.backgroundColor(context),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(S.current.QIANBAOMINGCHENG,style: YSColors.contentStyle(context),),
                            YSInputView(tips: S.current.SHEZHIQIANBAOMINGCHENG,valueSetter: (value){
                              _name = value;
                            },),
                            Padding(
                              padding: EdgeInsets.only(top: hsp(10)),
                              child: Text(S.current.KEYSTOREMIMA,style: YSColors.contentStyle(context),),
                            ),
                            YSInputView(tips: S.current.QINGSHURUKEYSTOREMIMA,valueSetter: (value){
                              _passWord = value;
                            },isSec: true,),
                            Padding(
                              padding: EdgeInsets.only(top: hsp(10)),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  YSCheckView(valueSetter: (value){
                                    _isAgree = value;
                                  }),
                                  Expanded(child: Text(' ${S.current.WOLIAOJIEBAKEWALLETWUFAWEIWOZHAOHUIMIMA}',style: YSColors.subStyle(context),),),

                                ],
                              ),
                            )
                          ],
                        ),
                      )
                  ),
                )
            ),
            Container(
              padding: EdgeInsets.only(top: hsp(20),bottom: hsp(20),left: hsp(20),right: hsp(20)),
              color: YSColors.backgroundColor(context),
              child: GestureDetector(
                onTap: (){
                  _postWordData();
                },
                child: Container(
                  height: hsp(50),
                  decoration: BoxDecoration(
                    color: YSColors.buttonColor(context),
                    borderRadius: const BorderRadius.all(Radius.circular(10)),
                  ),
                  alignment: Alignment.center,
                  child: Text(S.current.DAORUQIANBAO,style: YSColors.buttonStyle(context),),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  _intoPage() {
    ysShowCenterAlertView(context, YSAlertMessage(callback: () async{
      if(YSData().isManager){
        List array = await YSSqflite().rawQuery();
        if(!mounted)return;
        YSSqflite.notifier(context, array);
        Navigator.popUntil(context, ModalRoute.withName("YSWalletManager"));
      }else{
        Navigator.of(context).pushAndRemoveUntil(CupertinoPageRoute(builder: (context){
          return const YSTabBar();
        }), (route) => false);
      }

    }),isTrans: true);
  }

  _postWordData() async{
    FocusScope.of(context).unfocus();
    if(_editingController.text.isEmpty){
      ysFlutterToast(S.current.QINGSHURUKEYSTORE);
      return;
    }
    if(_name.isEmpty){
      ysFlutterToast(S.current.SHEZHIQIANBAOMINGCHENG);
      return;
    }
    if(_passWord.isEmpty){
      ysFlutterToast(S.current.QINGSHURUKEYSTOREMIMA);
      return;
    }
    if(!_isAgree){
      ysFlutterToast(S.current.DIANJIXUANZHONGLIAOJIEMIMAZHAOHUI);
      return;
    }
    YSData().name = _name;
    YSData().password = _passWord;
    bool isDone = false;
    isDone = await YSBipManager.setKeystore(_editingController.text,_passWord,isInto: true);
    if(isDone==true&&mounted){
      _intoPage();
    }
  }
}

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

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

class YSWalletObserveState extends State<YSWalletObserve> {
  String _name = '';
  bool _isAgree = false;
  final TextEditingController _editingController = TextEditingController();
  @override
  void dispose() {
    _editingController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ysTitle: S.current.GUANCHAQIANBAO,
      ysRightWidget: GestureDetector(
        onTap: () async{
          String result = await YSCodeImage.scanCode();
          _editingController.text = result;
          setState(() {});
        },
        child: Image.asset(YSColors.imageStyle(context, '编组 25'),height: hsp(20),width: hsp(20),),
      ),
      ysChild: Padding(
        padding: EdgeInsets.only(top: hsp(20),left: hsp(15),right: hsp(15)),
        child: Column(
          children: [
            Container(
              height: hsp(130),
              width: ysWidth(context)-hsp(30),
              decoration: const BoxDecoration(
                color: Color(0xFFF7F8FA),
                borderRadius: BorderRadius.all(Radius.circular(5))
              ),
              child: CupertinoTextField(
                placeholder:S.current.QINGSHURUGUANCHAQIANBAODIZHI,
                placeholderStyle: TextStyle(fontSize: zsp(18),color: YSColors.lineColor(context)),
                maxLines: 100,
                textDirection: TextDirection.ltr,
                decoration: const BoxDecoration(),
                padding: EdgeInsets.all(hsp(10)),
                controller: _editingController,
              ),
            ),
            Padding(
              padding: EdgeInsets.only(top: hsp(15)),
              child: Text(S.current.GUANCHAQIANBAOBUXUYAODAORUSIYAOSHI,style: YSColors.subStyle(context),),
            ),
            Container(
              padding: EdgeInsets.only(top: hsp(15)),
              alignment: Alignment.centerLeft,
              child: Text(S.current.QIANBAOMINGCHENG,style: YSColors.contentStyle(context),),
            ),
            YSInputView(tips: S.current.SHEZHIQIANBAOMINGCHENG,valueSetter: (value){
              _name = value;
            },),
            Padding(
              padding: EdgeInsets.only(top: hsp(20)),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  YSCheckView(valueSetter: (value){
                    _isAgree = value;
                  }),
                  Expanded(child: Text(' ${S.current.WOLIAOJIEBAKEWALLETWUFAWEIWOZHAOHUIMIMA}',style: YSColors.subStyle(context),),),
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.only(top: hsp(20),bottom: hsp(20)),
              color: YSColors.backgroundColor(context),
              child: GestureDetector(
                onTap: (){
                  _postWordData();
                },
                child: Container(
                  height: hsp(50),
                  decoration: BoxDecoration(
                    color: YSColors.buttonColor(context),
                    borderRadius: const BorderRadius.all(Radius.circular(10)),
                  ),
                  alignment: Alignment.center,
                  child: Text(S.current.DAORUQIANBAO,style: YSColors.buttonStyle(context),),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  _intoPage() {
    ysShowCenterAlertView(context, YSAlertMessage(callback: () async{
      if(YSData().isManager){
        List array = await YSSqflite().rawQuery();
        if(!mounted)return;
        YSSqflite.notifier(context, array);
        Navigator.popUntil(context, ModalRoute.withName("YSWalletManager"));
      }else{
        Navigator.of(context).pushAndRemoveUntil(CupertinoPageRoute(builder: (context){
          return const YSTabBar();
        }), (route) => false);
      }

    }),isTrans: true);
  }

  _postWordData() async{
    FocusScope.of(context).unfocus();
    if(_editingController.text.isEmpty){
      ysFlutterToast(S.current.QINGSHURUKEYSTORE);
      return;
    }
    if(_name.isEmpty){
      ysFlutterToast(S.current.SHEZHIQIANBAOMINGCHENG);
      return;
    }
    if(!_isAgree){
      ysFlutterToast(S.current.DIANJIXUANZHONGLIAOJIEMIMAZHAOHUI);
      return;
    }
    YSData().name = _name;
    YSData().password = '';
    bool isDone = false;
    isDone = await YSBipManager.setObserve(_editingController.text,isInto: true);
    if(isDone==true&&mounted){
      _intoPage();
    }
  }
}




