import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wallet/generated/l10n.dart';
import 'package:flutter_wallet/login/YSChooseIntoType.dart';
import 'package:flutter_wallet/tools/YSNetWork.dart';
import 'package:flutter_wallet/tools/YSRefrehLoad.dart';
import 'package:flutter_wallet/tools/YSSqflite.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:sqflite/sqflite.dart';
import '../login/YSWalletCreate.dart';
import '../login/YSWalletInto.dart';
import '../wallet/view/YSWalletListItemView.dart';
import 'YSColors.dart';
import 'YSTools.dart';
import 'YSVersionView.dart';

class YSTipsAlertView2 extends StatefulWidget {
  final ValueSetter<bool> valueSetter;
  final String tipsStr;
  final String tipsStr2;
  final String? btnStr1;
  final String? btnStr2;
  const YSTipsAlertView2({Key? key, required this.valueSetter, this.tipsStr = '', this.tipsStr2 = '', this.btnStr1, this.btnStr2}) : super(key: key);

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

class YSTipsAlertView2State extends State<YSTipsAlertView2> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: ysWidth(context)-hsp(60),
        height: hsp(205),
        decoration: BoxDecoration(
            color: YSColors.backgroundColor(context),
            borderRadius: const BorderRadius.all(Radius.circular(10))
        ),
        child: LayoutBuilder(
          builder: (context,conSize){
            return Column(
              children: [
                Padding(
                  padding: EdgeInsets.only(left: hsp(15),right: hsp(15)),
                  child: Column(
                    children: [
                      Container(
                          margin: EdgeInsets.only(top: hsp(30),bottom: hsp(15)),
                          height: hsp(30),
                          alignment: Alignment.center,
                          child: Image.asset(YSColors.imageStyle(context, '警告'),height: hsp(30),width: hsp(30),)
                      ),
                      SizedBox(
                        height: hsp(20),
                        child: Text(widget.tipsStr,style: YSColors.contentStyle(context),maxLines: 3,overflow: TextOverflow.ellipsis,),
                      ),
                      Container(
                        height: hsp(60),
                        alignment: Alignment.center,
                        child: Text(widget.tipsStr2,style: YSColors.subStyle(context),maxLines: 3,overflow: TextOverflow.ellipsis,),
                      ),
                    ],
                  ),
                ),
                Container(
                  height: hsp(50),
                  decoration: BoxDecoration(
                      border: Border(top: BorderSide(color: YSColors.lineColor(context),width: 0.5))
                  ),
                  child: ListView.separated(
                    itemBuilder: (context,index){
                      return GestureDetector(
                        onTap: (){
                          Navigator.pop(context);
                          widget.valueSetter(index==1);
                        },
                        behavior: HitTestBehavior.opaque,
                        child: Container(
                          width: conSize.maxWidth/2-0.25,
                          alignment: Alignment.center,
                          child: Text(
                            index==0?widget.btnStr1??S.current.FOU:widget.btnStr2??S.current.SHI,
                            style: index==0?YSColors.contentStyle(context):TextStyle(fontSize: zsp(14),color: Colors.blue,
                                fontWeight: FontWeight.normal,decoration: TextDecoration.none),
                          ),
                        ),
                      );
                    },
                    separatorBuilder: (context,index){
                      return Container(width: 0.5,color:YSColors.lineColor(context),);
                    },
                    itemCount: 2,
                    scrollDirection: Axis.horizontal,
                  ),
                )
              ],
            );
          },
        ),
      ),
    );
  }
}

class YSTipsAlertView extends StatefulWidget {
  final ValueSetter<bool> valueSetter;
  final String tipsStr;
  final String? btnStr1;
  final String? btnStr2;
  const YSTipsAlertView({Key? key, required this.valueSetter, this.tipsStr = '', this.btnStr1, this.btnStr2}) : super(key: key);

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

class YSTipsAlertViewState extends State<YSTipsAlertView> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: ysWidth(context)-hsp(60),
        height: hsp(150),
        decoration: BoxDecoration(
            color: YSColors.backgroundColor(context),
            borderRadius: const BorderRadius.all(Radius.circular(10))
        ),
        child: LayoutBuilder(
          builder: (context,conSize){
            return Column(
              children: [
                Container(
                  height: hsp(100),
                  alignment: Alignment.center,
                  padding: EdgeInsets.all(hsp(10)),
                  child: Text(widget.tipsStr,style: YSColors.contentStyle(context),maxLines: 3,overflow: TextOverflow.ellipsis,),
                ),
                Container(
                  height: hsp(50),
                  decoration: BoxDecoration(
                      border: Border(top: BorderSide(color: YSColors.lineColor(context),width: 0.5))
                  ),
                  child: ListView.separated(
                    itemBuilder: (context,index){
                      return GestureDetector(
                        onTap: (){
                          Navigator.pop(context);
                          widget.valueSetter(index==1);
                        },
                        behavior: HitTestBehavior.opaque,
                        child: Container(
                          width: conSize.maxWidth/2-0.25,
                          alignment: Alignment.center,
                          child: Text(
                            index==0?widget.btnStr1??S.current.FOU:widget.btnStr2??S.current.SHI,
                            style: YSColors.contentStyle(context),
                          ),
                        ),
                      );
                    },
                    separatorBuilder: (context,index){
                      return Container(width: 0.5,color:YSColors.lineColor(context),);
                    },
                    itemCount: 2,
                    scrollDirection: Axis.horizontal,
                  ),
                )
              ],
            );
          },
        ),
      ),
    );
  }
}
class YSAlertMessage extends StatefulWidget {
  final Icon? icon;
  final String? message;
  final VoidCallback? callback;
  const YSAlertMessage({Key? key, this.icon, this.message, required this.callback}) : super(key: key);

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

class YSAlertMessageState extends State<YSAlertMessage> {
  @override
  void initState() {
    Future.delayed(const Duration(seconds: 1)).then((value) {
      _clearAlert();
    });
    super.initState();
  }

  _clearAlert() {
    Navigator.pop(context);
    if(widget.callback!=null){
      widget.callback!();
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async{
        return false;
      },
      child: Center(
        child: GestureDetector(
          onTap: (){
            // _clearAlert();
          },
          child: Container(
            height: hsp(150),
            width: hsp(150),
            decoration: BoxDecoration(
                borderRadius: const BorderRadius.all(Radius.circular(10)),
                color: YSColors.alertColor(context)
            ),
            alignment: Alignment.center,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                widget.icon??Icon(Icons.check,size: hsp(50),color: YSColors.contain2Color(context),),
                Padding(
                  padding: EdgeInsets.only(top: hsp(5)),
                  child: Text(widget.message??'验证成功',style: YSColors.content2Style(context),),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class YSChooseWalletType extends StatelessWidget {
  final bool isHome;
  const YSChooseWalletType({Key? key, this.isHome = false}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: ysWidth(context),
      height: hsp(300),
      decoration: BoxDecoration(
        color: YSColors.containColor(context),
        borderRadius: const BorderRadius.only(topRight: Radius.circular(35),topLeft: Radius.circular(35))
      ),
      child: Column(
        children: [
          Container(
            margin: EdgeInsets.only(top: hsp(15)),
            height: hsp(4),
            width: hsp(71),
            decoration: BoxDecoration(
              color: YSColors.lineColor(context),
              borderRadius: const BorderRadius.all(Radius.circular(50))
            ),
          ),
          Container(
            height: hsp(50),
            alignment: Alignment.center,
            child: Text(S.current.QIANBAOGUANLI2,style: YSColors.titleStyle(context),),
          ),
          Padding(
            padding: EdgeInsets.only(top: hsp(10)),
            child: SizedBox(
              width: ysWidth(context)-hsp(40),
              child: ListView.separated(
                itemBuilder: (context,index){
                  return Container(
                    padding: EdgeInsets.all(hsp(15)),
                    decoration: BoxDecoration(
                        color: index==0?Colors.white:Colors.black,
                        borderRadius: const BorderRadius.all(Radius.circular(10)),
                        boxShadow: [
                          BoxShadow(color: YSColors.shadowColor(context),blurRadius: 3)
                        ]
                    ),
                    child: Row(
                      children: [
                        Image.asset(YSColors.imageStyle(context, index==0?'信用卡 2':'资管信托 2'),height: hsp(40),width: hsp(60),),
                        Expanded(
                            child: Padding(
                              padding: EdgeInsets.only(left: hsp(10),right: hsp(10)),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    index==0?S.current.MEIYOUQIANBAO:S.current.YIYOUQIANBAO,
                                    style: index==0?YSColors.contentStyle(context):YSColors.content2Style(context),
                                  ),
                                  Text(
                                    index==0?S.current.CHUANGJIANQUKUAILIANQIANBAO:S.current.DAORUQIANBAO,
                                    style: YSColors.subStyle(context),
                                  )
                                ],
                              ),
                            )
                        ),
                        GestureDetector(
                          onTap: (){
                            if(isHome==false)Navigator.of(context).pop();
                            if(index==0){
                              Navigator.of(context).push(
                                  CupertinoPageRoute(builder: (context){
                                    return const YSWalletCreate();
                                  })
                              );
                            }else{
                              Navigator.of(context).push(
                                  CupertinoPageRoute(builder: (context){
                                    return const YSChooseIntoType();
                                  })
                              );
                            }
                          },
                          behavior: HitTestBehavior.opaque,
                          child: Container(
                            height: hsp(30),
                            width: hsp(120),
                            decoration:  BoxDecoration(
                                borderRadius: const BorderRadius.all(Radius.circular(10)),
                                color: index==0?Colors.black:Colors.white
                            ),
                            alignment: Alignment.center,
                            child: Text(
                              index==0?S().CHUANGJIANQIANBAO:S().DAORUQIANBAO,
                              style: index==0?YSColors.sub2Style(context):YSColors.sub4Style(context),
                            ),
                          ),
                        )
                      ],
                    ),
                  );
                },
                separatorBuilder: (context,index){
                  return Container(height: hsp(25),);
                },
                itemCount: 2,
                shrinkWrap: true,
                padding: const EdgeInsets.all(0),
                physics: const NeverScrollableScrollPhysics(),
              ),
            ),
          )
        ],
      ),
    );
  }
}





///TODO::网络选择
class YSWalletNetView extends StatefulWidget {
  final ValueSetter? valueSetter;
  const YSWalletNetView({Key? key, this.valueSetter}) : super(key: key);

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

class YSWalletNetViewState extends State<YSWalletNetView> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: hsp(400),
      decoration: BoxDecoration(
          color: YSColors.containColor(context),
          borderRadius: const BorderRadius.only(topRight: Radius.circular(10),topLeft: Radius.circular(10))
      ),
      child: Column(
        children: [
          Container(
            height: hsp(49),
            alignment: Alignment.center,
            child: Text(S.current.XUANZEWANGLUO,style: YSColors.titleStyle(context),),
          ),
          Divider(height: hsp(1),color: YSColors.lineColor(context),),
          SizedBox(
            height: hsp(350),
            child: YSRefreshLoad(
              url: 'chains',
              request: const {},
              postData: (value) {
                YSData().typeArray = value;
                setState(() {});
              },
              child: ListView.separated(
                itemBuilder: (context,index){
                  Map item = YSData().typeArray[index];
                  bool isChoose = item['id'] == YSData().typeId;
                  return GestureDetector(
                    onTap: () async{
                      if(widget.valueSetter!=null){
                          widget.valueSetter!(item);
                      }else{
                        YSData().chooseIndex++;
                        YSData().typeId = item['id'];
                        setState(() {});
                        YSData().exName = item['name'];
                        List nodes = item['nodes']??[];
                        if(nodes.isNotEmpty){
                          YSData().rpc = nodes.first['rpc_url'];
                        }
                        YSSharedPreferences.setStringValue('typeId', item['id']);
                        YSSharedPreferences.setStringValue('typeName', item['name']);
                        YSSqflite sq = YSSqflite().init();
                        List array = await sq.rawQuery();
                        if(!mounted)return;
                        if(array.isNotEmpty){
                          LogUtil.d(array);
                          Map wallet= array.firstWhere((element) => element['chooseType']==1,orElse: ()=>{});
                          if(wallet.isEmpty)wallet = array.first;
                          YSData().wallet = wallet;
                          YSSqflite.notifier(context, array);
                          addToken(context,kind: 1);
                          checkAddress();
                        }else{
                          YSSqflite.notifier(context, array);
                        }
                        // getMindToken(context);
                      }
                      if(!mounted)return;
                      Navigator.pop(context);
                    },
                    behavior: HitTestBehavior.opaque,
                    child: Container(
                      height: hsp(49),
                      padding: EdgeInsets.only(left: hsp(15),right: hsp(15)),
                      child: Row(
                        children: [
                          SizedBox(
                            height: hsp(40),
                            width: hsp(40),
                            child: YSImage.network(item['icon'],),
                          ),
                          Expanded(
                            child: Text(' ${item['name']}',style: YSColors.contentStyle(context),)
                          ),
                          if(isChoose)Image.asset(YSColors.imageStyle(context, '矩形2'),height: hsp(15),width: hsp(15),),
                        ],
                      ),
                    ),
                  );
                },
                separatorBuilder: (context,index){
                  return Divider(height: hsp(1),color: YSColors.lineColor(context),);
                },
                itemCount: YSData().typeArray.length
              ),
            ),
          )
        ],
      ),
    );
  }
}

class YSLoadView extends StatefulWidget {
  final Function() funAction;
  final Map wallet;
  final String? successStr;
  final String? failStr;
  final ValueSetter? valueSetter;
  const YSLoadView({Key? key, required this.funAction, required this.wallet, this.successStr, this.failStr, this.valueSetter}) : super(key: key);

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

class YSLoadViewState extends State<YSLoadView> {
  int _type = 3;
  final TextEditingController _editingController = TextEditingController();
  @override
  void dispose() {
    _editingController.dispose();
    super.dispose();
  }
  _postFunction() async{
    bool result = await widget.funAction();
    if(result){
      _type = 1;
    }else{
      _type = 2;
    }
    setState(() {});
    Future.delayed(const Duration(seconds: 1)).then((value) {
      Navigator.pop(context);
      if(widget.valueSetter!=null){
        widget.valueSetter!(_type);
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async{
        return true;
      },
      child: Padding(
          padding:EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom,),
          child: Center(
          child: _type==3?Container(
            height: hsp(200),
            width: ysWidth(context)-hsp(100),
            decoration: BoxDecoration(
              color: YSColors.containColor(context),
              borderRadius: const BorderRadius.all(Radius.circular(10))
            ),
            child: Column(
              children: [
                Container(
                  height: hsp(49),
                  decoration: BoxDecoration(
                      border: Border(bottom: BorderSide(color: YSColors.lineColor(context),width: hsp(1)))
                  ),
                  alignment: Alignment.center,
                  child: Text(S.current.MIMAYANZHENG,style: YSColors.contentStyle(context),),
                ),
                Container(
                  height: hsp(100),
                  padding: EdgeInsets.only(left: hsp(15),right: hsp(15),top: hsp(25),bottom: hsp(25)),
                  child: CupertinoTextField(
                      placeholder: S.current.QINGSHURMIMA,
                      style: YSColors.contentStyle(context),
                      padding: EdgeInsets.only(left: hsp(10),right: hsp(10)),
                      controller: _editingController,
                      obscureText: true
                  ),
                ),
                Container(
                  height: hsp(49),
                  decoration: BoxDecoration(
                      border: Border(top: BorderSide(color: YSColors.lineColor(context),width: hsp(1)))
                  ),
                  child: Row(
                    children: [
                      Expanded(child: GestureDetector(
                        onTap: (){
                          Navigator.pop(context);
                        },
                        behavior: HitTestBehavior.opaque,
                        child: Align(
                          alignment: Alignment.center,
                          child: Text(S.current.QUXIAO,style: YSColors.contentStyle(context),),
                        ),
                      )),
                      Container(width: hsp(1),color: YSColors.lineColor(context),),
                      Expanded(child: GestureDetector(
                        onTap: (){
                          FocusScope.of(context).unfocus();
                          if(_editingController.text.isEmpty){
                            ysFlutterToast(S.current.MIMABUNENGWEIKONG);
                            return;
                          }
                          if(_editingController.text!=widget.wallet['password']){
                            ysFlutterToast(S.current.MIMACUOWU);
                            return;
                          }
                          _type = 0;
                          setState(() {});
                          _postFunction();
                        },
                        behavior: HitTestBehavior.opaque,
                        child: Align(
                          alignment: Alignment.center,
                          child: Text(S.current.QUEDING,style: YSColors.contentStyle(context),),
                        ),
                      ))
                    ],
                  ),
                ),
              ],
            ),
          ):Container(
            height: hsp(150),
            width: hsp(150),
            decoration: BoxDecoration(
                borderRadius: const BorderRadius.all(Radius.circular(10)),
                color: YSColors.alertColor(context)
            ),
            alignment: Alignment.center,
            child:  _type==0?CircularProgressIndicator(
              color: YSColors.containColor(context),
            ):Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(_type==1?Icons.check:Icons.error,size: hsp(50),color: YSColors.contain2Color(context),),
                Padding(
                  padding: EdgeInsets.only(top: hsp(5)),
                  child: Text(_type==1?(widget.successStr??S.current.ZHUANZHANGTIJIAOCHENGGONG):(widget.failStr??S.current.ZHUANZHANGTIJIAOSHIBAI),style: YSColors.content2Style(context),),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class YSLoadView2 extends StatefulWidget {
  final Function() funAction;
  final ValueSetter? valueSetter;
  final String? successStr;
  final String? failStr;
  const YSLoadView2({Key? key, required this.funAction, this.valueSetter, this.successStr, this.failStr}) : super(key: key);

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

class YSLoadView2State extends State<YSLoadView2> {
  int _type = 0;
  _postFunction() async{
    bool result = await widget.funAction();
    if(result){
      _type = 1;
    }else{
      _type = 2;
    }
    setState(() {});
    Future.delayed(const Duration(seconds: 0)).then((value) {
      widget.valueSetter!(result);
      Navigator.pop(context);
    });
  }
  @override
  void initState() {
    _postFunction();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        height: hsp(150),
        width: hsp(150),
        decoration: BoxDecoration(
            borderRadius: const BorderRadius.all(Radius.circular(10)),
            color: YSColors.alertColor(context)
        ),
        alignment: Alignment.center,
        child: _type==0?CircularProgressIndicator(
          color: YSColors.containColor(context),
        ):Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(_type==1?Icons.check:Icons.error,size: hsp(50),color: YSColors.contain2Color(context),),
            Padding(
              padding: EdgeInsets.only(top: hsp(5)),
              child: Text(_type==1?(widget.successStr??S.current.ZHUANZHANGTIJIAOCHENGGONG):(widget.failStr??S.current.ZHUANZHANGTIJIAOSHIBAI),style: YSColors.content2Style(context),),
            )
          ],
        ),
      ),
    );
  }
}


class YSChooseCheckView extends StatelessWidget {
  final ValueSetter<bool> valueSetter;
  const YSChooseCheckView({Key? key, required this.valueSetter}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: hsp(150),
      width: ysWidth(context),
      decoration: BoxDecoration(
        color: YSColors.containColor(context),
        borderRadius: const BorderRadius.only(topRight: Radius.circular(10),topLeft: Radius.circular(10))
      ),
      child: Column(
        children: [
          Container(
            height: hsp(50),
            alignment: Alignment.center,
            decoration: BoxDecoration(
                border: Border(bottom: BorderSide(color: YSColors.lineColor(context),width: hsp(1)))
            ),
            child: Text('类 型 选 择',style: TextStyle(fontSize: zsp(18),color: Colors.black),),
          ),
          SizedBox(
            height: hsp(100),
            child: ListView.builder(
              itemBuilder: (context,index){
                return GestureDetector(
                  onTap: (){
                    if(index==0){

                    }else{

                    }
                  },
                  behavior: HitTestBehavior.opaque,
                  child: Container(
                    height: hsp(50),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      border: Border(bottom: BorderSide(color: YSColors.lineColor(context),width: hsp(1)))
                    ),
                    child: Text('指纹验证',style: YSColors.contentStyle(context),),
                  ),
                );
              },
              itemCount: 2,
              padding: EdgeInsets.only(left: hsp(15),right: hsp(15)),
            ),
          )
        ],
      ),
    );
  }
}


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

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

class YSInputPasswordViewState extends State<YSInputPasswordView> {
  final TextEditingController _editingController = TextEditingController();
  @override
  void dispose() {
    _editingController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Padding(
        padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom,),
        child: Container(
          height: hsp(200),
          width: ysWidth(context)-hsp(100),
          decoration: BoxDecoration(
              color: YSColors.containColor(context),
              borderRadius: const BorderRadius.all(Radius.circular(10))
          ),
          child: Column(
            children: [
              Container(
                height: hsp(49),
                decoration: BoxDecoration(
                  border: Border(bottom: BorderSide(color: YSColors.lineColor(context),width: hsp(1)))
                ),
                alignment: Alignment.center,
                child: Text(S.current.MIMAYANZHENG,style: YSColors.contentStyle(context),),
              ),
              Container(
                height: hsp(100),
                padding: EdgeInsets.only(left: hsp(15),right: hsp(15),top: hsp(25),bottom: hsp(25)),
                child: CupertinoTextField(
                  placeholder: S.current.QINGSHURMIMA,
                  style: YSColors.contentStyle(context),
                  padding: EdgeInsets.only(left: hsp(10),right: hsp(10)),
                  controller: _editingController,
                  obscureText: true
                ),
              ),
              Container(
                height: hsp(49),
                decoration: BoxDecoration(
                    border: Border(top: BorderSide(color: YSColors.lineColor(context),width: hsp(1)))
                ),
                child: Row(
                  children: [
                    Expanded(child: GestureDetector(
                      onTap: (){
                        Navigator.pop(context);
                      },
                      behavior: HitTestBehavior.opaque,
                      child: Align(
                        alignment: Alignment.center,
                        child: Text(S.current.QUXIAO,style: YSColors.contentStyle(context),),
                      ),
                    )),
                    Container(width: hsp(1),color: YSColors.lineColor(context),),
                    Expanded(child: GestureDetector(
                      onTap: (){
                        if(_editingController.text.isEmpty){
                          ysFlutterToast(S.current.MIMABUNENGWEIKONG);
                          return;
                        }
                        if(_editingController.text!=widget.wallet['password']){
                          ysFlutterToast(S.current.MIMACUOWU);
                          return;
                        }
                        Navigator.pop(context);
                        widget.callback();
                      },
                      behavior: HitTestBehavior.opaque,
                      child: Align(
                        alignment: Alignment.center,
                        child: Text(S.current.QUEDING,style: YSColors.contentStyle(context),),
                      ),
                    ))
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class YSInputNetView extends StatefulWidget {
  final ValueSetter valueSetter;
  const YSInputNetView({Key? key, required this.valueSetter}) : super(key: key);

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

class YSInputNetViewState extends State<YSInputNetView> {
  final TextEditingController _editingController = TextEditingController();
  @override
  void dispose() {
    _editingController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Padding(
        padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom,),
        child: Container(
          height: hsp(200),
          width: ysWidth(context)-hsp(100),
          decoration: BoxDecoration(
              color: YSColors.containColor(context),
              borderRadius: const BorderRadius.all(Radius.circular(10))
          ),
          child: Column(
            children: [
              Container(
                height: hsp(49),
                alignment: Alignment.center,
                child: Text('添加自定义节点',style: YSColors.contentStyle(context),),
              ),
              Container(
                height: hsp(100),
                padding: EdgeInsets.only(left: hsp(15),right: hsp(15),top: hsp(10),bottom: hsp(10)),
                child: CupertinoTextField(
                  placeholder: '请输入节点地址',
                  style: YSColors.subStyle(context),
                  padding: EdgeInsets.all(hsp(10)),
                  controller: _editingController,
                  decoration: BoxDecoration(
                    color: YSColors.backgroundColor(context),
                    borderRadius: const BorderRadius.all(Radius.circular(5))
                  ),
                  maxLines: 10,
                ),
              ),
              Container(
                height: hsp(49),
                decoration: BoxDecoration(
                    border: Border(top: BorderSide(color: YSColors.lineColor(context),width: hsp(1)))
                ),
                child: Row(
                  children: [
                    Expanded(child: GestureDetector(
                      onTap: (){
                        Navigator.pop(context);
                      },
                      behavior: HitTestBehavior.opaque,
                      child: Align(
                        alignment: Alignment.center,
                        child: Text(S.current.QUXIAO,style: YSColors.contentStyle(context),),
                      ),
                    )),
                    Container(width: hsp(1),color: YSColors.lineColor(context),),
                    Expanded(child: GestureDetector(
                      onTap: (){
                        if(_editingController.text.isEmpty){
                          ysFlutterToast('节点不能为空');
                          return;
                        }
                        widget.valueSetter(_editingController.text);
                        Navigator.pop(context);
                      },
                      behavior: HitTestBehavior.opaque,
                      child: Align(
                        alignment: Alignment.center,
                        child: Text(S.current.QUEDING,style: YSColors.contentStyle(context),),
                      ),
                    ))
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}


///TODO::兑换选择
class YSExchangeTypeChooseView1 extends StatefulWidget {
  final Map? item;
  final ValueSetter valueSetter;
  const YSExchangeTypeChooseView1({Key? key, required this.valueSetter, this.item}) : super(key: key);

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

class YSExchangeTypeChooseView1State extends State<YSExchangeTypeChooseView1> with SingleTickerProviderStateMixin{
  List _titles = [];
  TabController? _tabController;
  @override
  void initState() {
    _getTitleArray();
    super.initState();
  }
  _getTitleArray() {
    _titles = YSData().typeArray;
    LogUtil.d(_titles);
    _tabController = TabController(length: _titles.length, vsync: this);
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      height: hsp(500),
      width: ysWidth(context),
      decoration: BoxDecoration(
          color: YSColors.containColor(context),
          borderRadius: const BorderRadius.only(topRight: Radius.circular(10),topLeft: Radius.circular(10))
      ),
      child: _tabController!=null?DefaultTabController(
        length: _titles.length,
        child: Column(
          children: [
            Container(
              height: hsp(50),
              alignment: Alignment.center,
              child: Text('钱 包 选 择',style: TextStyle(fontSize: zsp(18),color: Colors.black),),
            ),
            widget.item==null?Column(
              children: [
                Container(
                  height: hsp(50),
                  width: ysWidth(context),
                  decoration: BoxDecoration(
                      border: Border(top: BorderSide(color: YSColors.lineColor(context),width: hsp(0)),bottom: BorderSide(color: YSColors.lineColor(context),width: hsp(0)))
                  ),
                  child: TabBar(
                    controller: _tabController,
                    indicatorColor: Colors.transparent,
                    indicatorWeight: hsp(1),
                    isScrollable: true,
                    labelPadding: EdgeInsets.only(left: hsp(15)),
                    labelColor: YSColors.selectedColor(context),
                    unselectedLabelColor: YSColors.unSelectedColor(context),
                    tabs: _titles.map((f) {
                      return Tab(
                        child: Container(
                          padding: EdgeInsets.only(left: hsp(10),right: hsp(10),top: hsp(5),bottom: hsp(5)),
                          child: Text(f['token_sort_name']),
                        ),
                      );
                    }).toList(),
                  ),
                ),
                SizedBox(
                  height: hsp(400),
                  child: TabBarView(
                    controller: _tabController,
                    children: _titles.map((f) {
                      return YSAlertChooseWalletListView(item: f, valueSetter: (value) {
                        value['parentName'] = f['name'];
                        value['parentIcon'] = f['icon'];
                        List nodes = f['nodes']??[];
                        if(nodes.isNotEmpty){
                          value['parentRpc'] = nodes.first['rpc_url'];
                        }
                        widget.valueSetter(value);
                        Navigator.pop(context);
                      },);
                    }).toList(),
                  ),
                )
              ],
            ):YSAlertChooseWalletListView(item: {'icon':widget.item?['parentIcon'],'name':widget.item?['parentName']}, valueSetter: (value) {
              widget.valueSetter(value);
              Navigator.pop(context);
            },)
          ],
        ),
      ):Container(),
    );
  }
}

class YSAlertChooseWalletListView extends StatefulWidget {
  final ValueSetter valueSetter;
  final Map item;
  const YSAlertChooseWalletListView({Key? key, required this.item, required this.valueSetter}) : super(key: key);

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

class YSAlertChooseWalletListViewState extends State<YSAlertChooseWalletListView> {
  List _dataArray = [];
  @override
  void initState() {
    _getWalletData();
    super.initState();
  }
  _getWalletData() async{
    _dataArray = await YSSqflite().init().rawQuery(name: widget.item['name']);
    setState(() {});
  }
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemBuilder: (context, index) {
        Map item = _dataArray[index];
        return Container(
            padding: EdgeInsets.only(left: hsp(15),right: hsp(15),top: hsp(10),bottom: hsp(10)),
            alignment: Alignment.centerLeft,
            child: Row(
              children: [
                ClipRRect(
                  borderRadius: const BorderRadius.all(Radius.circular(50)),
                  child: YSImage.network(widget.item['icon'],height: hsp(40),width: hsp(40),),
                ),
                Expanded(
                    child: Container(
                      padding: EdgeInsets.only(top: hsp(5),bottom: hsp(5),left: hsp(5),right: hsp(5)),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(item['name']??'',style: YSColors.contentStyle(context),maxLines: 1,),
                          Padding(
                              padding: EdgeInsets.only(top: hsp(5)),
                              child: Text(ysTextCut(item['public']),style: YSColors.subStyle(context),)
                          )
                        ],
                      ),
                    )
                ),
                GestureDetector(
                  onTap: () async{
                    widget.valueSetter({'icon_url':widget.item['icon'],'bl_symbol':widget.item['name'],'address':item['public'],'private':item['private']});
                  },
                  child: Icon(Icons.add_circle_outline,size: hsp(20),color: Colors.blue,),
                )
              ],
            )
        );
      },
      itemCount: _dataArray.length,
      padding: const EdgeInsets.all(0),
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
    );
  }
}


class YSExchangeTypeChooseView2 extends StatefulWidget {
  final ValueSetter valueSetter;
  const YSExchangeTypeChooseView2({Key? key, required this.valueSetter}) : super(key: key);

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

class YSExchangeTypeChooseView2State extends State<YSExchangeTypeChooseView2> with SingleTickerProviderStateMixin{
  List _titles = [];
  TabController? _tabController;
  List _dataArray = [];
  Map _type = {};
  @override
  void initState() {
    _getTitleArray();
    super.initState();
  }
  _getTitleArray() {
    _titles = YSData().typeArray;
    _tabController = TabController(length: _titles.length, vsync: this);
    _type = _titles.firstWhere((element) => element['id']==YSData().typeId);
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      height: hsp(500),
      width: ysWidth(context),
      decoration: BoxDecoration(
          color: YSColors.containColor(context),
          borderRadius: const BorderRadius.only(topRight: Radius.circular(10),topLeft: Radius.circular(10))
      ),
      child: _tabController!=null?DefaultTabController(
        length: _titles.length,
        child: false?Column(
          children: [
            Container(
              height: hsp(50),
              alignment: Alignment.center,
              child: Text('类 型 选 择',style: TextStyle(fontSize: zsp(18),color: Colors.black),),
            ),
            Container(
              height: hsp(50),
              width: ysWidth(context),
              decoration: BoxDecoration(
                  border: Border(top: BorderSide(color: YSColors.lineColor(context),width: hsp(0)),bottom: BorderSide(color: YSColors.lineColor(context),width: hsp(0)))
              ),
              child: TabBar(
                controller: _tabController,
                indicatorColor: Colors.transparent,
                indicatorWeight: hsp(1),
                isScrollable: true,
                labelPadding: EdgeInsets.only(left: hsp(15)),
                labelColor: YSColors.selectedColor(context),
                unselectedLabelColor: YSColors.unSelectedColor(context),
                tabs: _titles.map((f) {
                  return Tab(
                    child: Container(
                      padding: EdgeInsets.only(left: hsp(10),right: hsp(10),top: hsp(5),bottom: hsp(5)),
                      child: Text(f['token_sort_name']),
                    ),
                  );
                }).toList(),
              ),
            ),
            SizedBox(
              height: hsp(400),
              child: TabBarView(
                controller: _tabController,
                children: _titles.map((f) {
                  return YSRefreshLoad(
                    url: 'tokens',
                    request: {'chain_id':f['id']},
                    postData: (value) {
                      _dataArray = value;
                      setState(() {});
                    },
                    isLoading: false,
                    child: ListView.builder(
                      itemBuilder: (context, index) {
                        Map item = _dataArray[index];
                        return YSWalletListChooseItemView(item: item,valueSetter: (value){
                          value['parentName'] = f['name'];
                          value['parentIcon'] = f['icon'];
                          List nodes = f['nodes']??[];
                          if(nodes.isNotEmpty){
                            value['parentRpc'] = nodes.first['rpc_url'];
                          }
                          widget.valueSetter(value);
                          Navigator.pop(context);
                        },);
                      },
                      itemCount: _dataArray.length,
                      padding: const EdgeInsets.all(0),
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                    ),
                  );
                }).toList(),
              ),
            )
          ],
        ):Column(
          children: [
            Container(
              height: hsp(50),
              alignment: Alignment.center,
              child: Text('类 型 选 择',style: TextStyle(fontSize: zsp(18),color: Colors.black),),
            ),
            SizedBox(
              height: hsp(450),
              child: YSRefreshLoad(
                url: 'tokens',
                request: {'chain_id':YSData().typeId},
                postData: (value) {
                  _dataArray = value;
                  setState(() {});
                },
                isLoading: false,
                child: ListView.builder(
                  itemBuilder: (context, index) {
                    Map item = _dataArray[index];
                    return YSWalletListChooseItemView(item: item,valueSetter: (value){
                      value['parentName'] = _type['name'];
                      value['parentIcon'] = _type['icon'];
                      List nodes = _type['nodes']??[];
                      if(nodes.isNotEmpty){
                        value['parentRpc'] = nodes.first['rpc_url'];
                      }
                      widget.valueSetter(value);
                      Navigator.pop(context);
                    },);
                  },
                  itemCount: _dataArray.length,
                  padding: const EdgeInsets.all(0),
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                ),
              ),
            )
          ],
        ),
      ):Container(),
    );
  }
}

///TODO::跨链选择
class YSCrossCoinChooseView extends StatefulWidget {
  final ValueSetter valueSetter;
  const YSCrossCoinChooseView({Key? key, required this.valueSetter}) : super(key: key);

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

class YSCrossCoinChooseViewState extends State<YSCrossCoinChooseView> {
  List _dataArray = [];
  @override
  Widget build(BuildContext context) {
    return Container(
      height: hsp(400),
      width: ysWidth(context),
      decoration: BoxDecoration(
          color: YSColors.containColor(context),
          borderRadius: const BorderRadius.only(topRight: Radius.circular(10),topLeft: Radius.circular(10))
      ),
      child: Column(
        children: [
          Container(
            height: hsp(50),
            alignment: Alignment.center,
            child: Text('资 产 选 择',style: TextStyle(fontSize: zsp(18),color: Colors.black),),
          ),
          SizedBox(
            height: hsp(350),
            child: YSRefreshLoad(
              url: 'tokens',
              request: {'chain_id':YSData().typeId},
              postData: (value) {
                _dataArray = value;
                setState(() {});
              },
              isLoading: false,
              child: ListView.builder(
                itemBuilder: (context, index) {
                  Map item = _dataArray[index];
                  return GestureDetector(
                    onTap: (){
                      widget.valueSetter(item);
                      Navigator.pop(context);
                    },
                    behavior: HitTestBehavior.opaque,
                    child: YSCrossListItemView(item: item,),
                  );
                },
                itemCount: _dataArray.length,
                padding: const EdgeInsets.all(0),
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
              ),
            ),
          )
        ],
      ),
    );
  }
}

class YSCrossAddressView extends StatefulWidget {
  final ValueSetter valueSetter;
  final Map item;
  const YSCrossAddressView({Key? key, required this.valueSetter, required this.item}) : super(key: key);

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

class YSCrossAddressViewState extends State<YSCrossAddressView> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: hsp(400),
      width: ysWidth(context),
      decoration: BoxDecoration(
          color: YSColors.containColor(context),
          borderRadius: const BorderRadius.only(topRight: Radius.circular(10),topLeft: Radius.circular(10))
      ),
      child: Column(
        children: [
          Container(
            height: hsp(50),
            alignment: Alignment.center,
            child: Text('类 型 选 择',style: TextStyle(fontSize: zsp(18),color: Colors.black),),
          ),
          SizedBox(
            height: hsp(350),
            child: YSAlertChooseWalletListView(item: widget.item, valueSetter: (value) {
              widget.valueSetter(value);
              Navigator.pop(context);
            },),
          )
        ],
      ),
    );
    ;
  }
}


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

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

class YSCrossListItemViewState extends State<YSCrossListItemView> {
  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.only(left: hsp(15),right: hsp(15),top: hsp(10),bottom: hsp(10)),
        alignment: Alignment.centerLeft,
        child: Row(
          children: [
            ClipRRect(
              borderRadius: const BorderRadius.all(Radius.circular(50)),
              child: YSImage.network(widget.item['icon_url'],height: hsp(40),width: hsp(40),),
            ),
            Expanded(
                child: Container(
                  padding: EdgeInsets.only(top: hsp(5),bottom: hsp(5),left: hsp(5),right: hsp(5)),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(widget.item['name']??'',style: YSColors.contentStyle(context),maxLines: 1,),
                      Padding(
                          padding: EdgeInsets.only(top: hsp(5)),
                          child: Text(ysTextCut(widget.item['address']),style: YSColors.subStyle(context),)
                      )
                    ],
                  ),
                )
            ),
          ],
        )
    );
  }
}

///版本更新
getVersionData(BuildContext context,Map data) async{
  if(data.isNotEmpty){
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    if(int.parse(packageInfo.buildNumber)<data['build']){
      // ignore: use_build_context_synchronously
      ysShowCenterAlertView(context, YSVersionView(
        title: 'v${data['version']}更新',
        content: data['txt'],
        url: data['url'],
        isBack: data['isForce']==0,
      ),isTrans: false,isBarr: data['isForce']==1);
    }
  }
}