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/YSPingView.dart';

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

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

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

class YSWalletRocChooseState extends State<YSWalletRocChoose> {
  List _dataArray = [];
  @override
  Widget build(BuildContext context) {
    return YSBase2(
      ysTitle: YSData().exName,
      ysChild: Column(
        children: [
          Container(
            margin: EdgeInsets.only(top: hsp(20)),
            width: ysWidth(context)-hsp(30),
            padding: EdgeInsets.all(hsp(10)),
            height: hsp(130),
            decoration: BoxDecoration(
              color: YSColors.containColor(context),
              borderRadius: const BorderRadius.all(Radius.circular(10))
            ),
            alignment: Alignment.center,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Text(S.current.JIEDIANSUDU,style: YSColors.contentStyle(context),),
                  ],
                ),
                Padding(
                  padding: EdgeInsets.only(top: hsp(5)),
                  child: Text(S.current.QUKUAIGAODUGAODUZHIYUEDA,style: YSColors.subStyle(context),),
                )
              ],
            ),
          ),
          Container(
            height: hsp(40),
            width: ysWidth(context)-hsp(30),
            alignment: Alignment.centerLeft,
            child: Text(S.current.RUIJIANJIEDIAN,style: YSColors.content2Style(context),),
          ),
          Container(
            height: ysHeight(context)-ysTOP(context)-hsp(300),
            decoration: BoxDecoration(
              color: YSColors.containColor(context),
              borderRadius: const BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10))
            ),
            width: ysWidth(context),
            child: YSRefreshLoad(
              url: 'rpcnodes',
              request: {'chain_id':YSData().typeId},
              postData: (value) {
                _dataArray = value;
                setState(() {});
              },
              child: ListView.builder(
                itemBuilder: (context, index) {
                  Map item = _dataArray[index];
                  bool isChoose = item['rpc_url']==YSData().rpc;
                  return GestureDetector(
                    onTap: (){
                      if(!isChoose){
                        YSData().rpc = item['rpc_url'];
                        setState(() {});
                      }
                    },
                    behavior: HitTestBehavior.opaque,
                    child: Container(
                      padding: EdgeInsets.only(top: hsp(10),bottom: hsp(10)),
                      decoration: BoxDecoration(
                        border: Border(bottom: BorderSide(color: YSColors.lineColor(context),width: hsp(1)))
                      ),
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Expanded(
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(item['chain_name'],style: YSColors.contentStyle(context),),
                                Padding(
                                  padding: EdgeInsets.only(top: hsp(5)),
                                  child: Text(item['rpc_url'],style: YSColors.subStyle(context),maxLines: 1,overflow: TextOverflow.ellipsis,),
                                )
                              ],
                            )
                          ),
                          const YSPingView(),
                          if(isChoose)Padding(
                            padding: EdgeInsets.only(left: hsp(10)),
                            child: Image.asset(YSColors.imageStyle(context, '矩形2'),height: hsp(15),width: hsp(15),color: YSColors.iconColor(context),),
                          ),
                        ],
                      ),
                    ),
                  );
                },
                itemCount: _dataArray.length,
                padding: EdgeInsets.only(left: hsp(15),right: hsp(15)),
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
              ),
            ),
          ),
          GestureDetector(
            onTap: (){
              ysShowCenterAlertView(context, YSInputNetView(valueSetter: (value){
                YSData().rpc = value;
              }));
            },
            behavior: HitTestBehavior.opaque,
            child: Container(
              height: hsp(50),
              width: ysWidth(context)-hsp(40),
              decoration: BoxDecoration(
                color: YSColors.buttonColor(context),
                borderRadius: const BorderRadius.all(Radius.circular(10))
              ),
              alignment: Alignment.center,
              child: Text(S.current.RIANJIAZIDINGYIJIEDIAN,style: YSColors.buttonStyle(context),),
            ),
          )
        ],
      ),
    );
  }
}
