import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wallet/base/YSTabBar.dart';
import 'package:flutter_wallet/login/YSGuide.dart';
import 'package:flutter_wallet/login/YSLoginAuth.dart';
import 'package:flutter_wallet/login/YSWalletSetting.dart';
import 'package:flutter_wallet/tools/YSNetWork.dart';

import '../tools/YSSqflite.dart';
import '../tools/YSTools.dart';

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

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

class YSLogoState extends State<YSLogo> {

  DateTime _startTime = DateTime.now();
  @override
  void initState() {
    _getBasicData();
    super.initState();
  }


  _getBasicData() async{
    YSNetWork.ysRequestHttp(context, type: RequestType.get, api: 'chains', parameter: {'page':'1','count':'10'}, successSetter: (dict) async{
      Map data = dict['data'];
      List list = data['list']??[];
      YSData ysData = YSData();
      ysData.typeArray = list;
      String typeId = await YSSharedPreferences.getStringValue('typeId');
      String typeName = await YSSharedPreferences.getStringValue('typeName');
      if(list.isNotEmpty){
        // for (var element in list) {
        //   String name = '${element['name']}'.replaceAll('-', '').replaceAll(' ', '');
        //   YSSqflite().init(name: name);
        // }
        Map type = list.first;
        if(typeId.isEmpty){
          YSSharedPreferences.setStringValue('typeId', type['id']);
          YSSharedPreferences.setStringValue('typeName', type['name']);
          ysData.typeId = type['id'];
          ysData.exName = type['name'];
        }else{
          ysData.typeId = typeId;
          ysData.exName = typeName;
          List list = ysData.typeArray.where((element) => element['id']==YSData().typeId).toList();
          if(list.isNotEmpty){
            type = list.first;
          }else{
            type = YSData().typeArray.first;
          }
        }
        List nodes = type['nodes']??[];
        if(nodes.isNotEmpty){
          Map node = nodes.first;
          ysData.rpc = node['rpc_url'];
        }
      }
      _into();
    });

  }

  _into() async{
    DateTime endTime = DateTime.now();
    int second = endTime.difference(_startTime).inSeconds;
    int second2 = (3-second)>0?(3-second):0;
    LogUtil.d('_into=======$second');
    Future.delayed(Duration(seconds: second2)).then((value) {
      Navigator.pushAndRemoveUntil(context, PageRouteBuilder(pageBuilder: (_, __, ___) =>
        YSUserRecord().install.isEmpty?const YSGuide():const YSLoginAuth(),
        transitionDuration: const Duration(seconds: 0),
      ),(route) => false);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: Image.asset('images/loge7.gif',height: hsp(200),width: hsp(200),),
      ),
    );
  }

}
