import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wallet/base/YSBase.dart';
import 'package:flutter_wallet/generated/l10n.dart';
import 'package:flutter_wallet/login/YSWalletInto.dart';
import 'package:flutter_wallet/tools/YSColors.dart';
import 'package:flutter_wallet/tools/YSTools.dart';

import '../tools/YSRefrehLoad.dart';

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

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

class YSChooseIntoTypeState extends State<YSChooseIntoType> {
  final List _chooseArray = [
    {'image':'私钥导入 (1)','title':S.current.SIYAODAORU,'subTitle':S.current.TONGGUOSHURUMINGWENSIYAO,'type':1},
    {'image':'导出助记词','title':S.current.ZHUJICIDAORU,'subTitle':S.current.TONGGUOSHURUZHUJICI,'type':2},
    {'image':'助记词导入','title':S.current.KEYSTOREDAORU,'subTitle':S.current.TONGGUOSHURUKEYSTORE,'type':3},
    {'image':'行业观察','title':S.current.GUANCHAQIANBAO,'subTitle':S.current.WUXUDAORUSIYAO,'type':4},
  ];
  bool _isAlert = true;
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ysTitle: S.current.DAORUFANGSHI,
      ysChild: SizedBox(
        width: ysWidth(context),
        height: ysHeight(context)-ysTOP(context)-70,
        child: Stack(
          children: [
            ListView.separated(
              itemBuilder: (context,index){
                Map item = _chooseArray[index];
                return GestureDetector(
                  onTap: (){
                    int type = item['type'];
                    Navigator.of(context).push(
                        CupertinoPageRoute(builder: (context){
                          return type==3?const YSWalletIntoKeystore():type==4?const YSWalletObserve():YSWalletIntoWord(type: type,);
                        })
                    );
                  },
                  behavior: HitTestBehavior.opaque,
                  child: Container(
                    padding: EdgeInsets.only(top: hsp(10),bottom: hsp(10)),
                    child: Row(
                      children: [
                        Image.asset(YSColors.imageStyle(context, item['image']),height: hsp(30),width: hsp(30),),
                        Expanded(child: Container(
                          padding: EdgeInsets.only(left: hsp(10),right: hsp(10)),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(item['title'],style: YSColors.contentStyle(context),),
                              Padding(
                                padding: EdgeInsets.only(top: hsp(5)),
                                child: Text(item['subTitle'],style: YSColors.subStyle(context),),
                              )
                            ],
                          ),
                        )),
                        Icon(Icons.chevron_right,size: hsp(30),color: YSColors.lineColor(context),)
                      ],
                    ),
                  ),
                );
              },
              separatorBuilder: (context,index){
                return Divider(height: hsp(1),color: YSColors.lineColor(context),);
              },
              itemCount: _chooseArray.length,
              padding: EdgeInsets.only(left: hsp(15),right: hsp(15)),
            ),
            if(_isAlert)Container(
              padding: EdgeInsets.only(top: ysHeight(context)-ysTOP(context)-70-hsp(400)),
              color: YSColors.shadowColor(context),
              child: 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{
                                  YSData().typeId = item['id'];
                                  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']);
                                  _isAlert = false;
                                  setState(() {});
                                },
                                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
                        ),
                      ),
                    )
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
