import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wallet/generated/l10n.dart';
import 'package:flutter_wallet/login/view/YSHelpView.dart';
import 'package:flutter_wallet/setting/YSAgreement.dart';
import 'package:flutter_wallet/tools/YSAlertView.dart';
import 'package:flutter_wallet/tools/YSColors.dart';
import 'package:flutter_wallet/tools/YSNetWork.dart';
import 'package:flutter_wallet/tools/YSTools.dart';
import 'package:package_info_plus/package_info_plus.dart';

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

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

class YSAboutUsState extends State<YSAboutUs> {
  final List _array = [
    {'icon':'编组 78','index':1},
    {'icon':'编组 68','index':2},
    {'icon':'编组 58','index':3},
    {'icon':'编组 48','index':4},
    {'icon':'编组 98','index':5},
    {'icon':'编组 28','index':6},
    {'icon':'编组 38','index':7}
  ];
  String _version = '';
  bool _hasNew = false;
  Map _data = {};
  @override
  void initState() {
    _getData();
    _getVersionData();
    super.initState();
  }
  _getVersionData() async{
    YSNetWork.ysRequestHttp(context, type: RequestType.post, api: 'versions', parameter: {}, successSetter: (dict) async{
      List list = dict['data']['list'];
      if(list.isNotEmpty){
        _data = list.first;
        PackageInfo packageInfo = await PackageInfo.fromPlatform();
        _hasNew = int.parse(packageInfo.buildNumber)<_data['build'];
        setState(() {});
      }
    });
  }
  _getData() async{
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    _version = packageInfo.version;
    setState(() {});
  }
  @override
  Widget build(BuildContext context) {
    List _menuArray = [
      {'title':S.current.BANBENGENGXIN,'value':_hasNew?'有新版本':'','type':1},
      {'title':S.current.GENGXINRIZHI,'value':'','type':2},
      {'title':S.current.YONGHUXIEYI,'value':'','type':2}
    ];
    return Scaffold(
      body: Container(
        height: ysHeight(context),
        width: ysWidth(context),
        child: Stack(
          children: [
            const YSHeadView(),
            Padding(
              padding: EdgeInsets.only(top: ysTOP(context)+hsp(20),left: hsp(20),right: hsp(20)),
              child: Column(
                children: [
                  Align(
                    alignment: Alignment.centerLeft,
                    child: GestureDetector(
                      onTap: (){
                        Navigator.pop(context);
                      },
                      child: Icon(Icons.chevron_left,size: hsp(30),color: Colors.white,)
                    ),
                  ),
                  Padding(
                      padding: EdgeInsets.only(top: hsp(50),bottom: hsp(30)),
                      child: Image.asset(YSColors.imageStyle(context, '资源 2'),height: hsp(40),width: hsp(70),color: Colors.white,)
                  ),
                  Image.asset(
                    YSColors.imageStyle(context, '资源 3'),
                    width: hsp(150),
                    height: hsp(30),
                    color: Colors.white,
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: hsp(20),bottom: hsp(60)),
                    child: Text('${S.current.DANGQIANBANBEN}：$_version',style: YSColors.subStyle(context),)
                  ),
                  Container(
                    height: hsp(135),
                    width: hsp(300),
                    decoration: BoxDecoration(
                      borderRadius: const BorderRadius.all(Radius.circular(10)),
                      color: YSColors.containColor(context),
                      boxShadow: [
                        BoxShadow(color: YSColors.lineColor(context),blurRadius: 3)
                      ],
                    ),
                    child: ListView.separated(
                      itemBuilder: (context,index){
                        Map item = _menuArray[index];
                        return GestureDetector(
                          onTap: (){
                            if(index==0){
                              if(_hasNew){
                                getVersionData(context,_data);
                              }else{
                                ysFlutterToast('暂无新版本');
                              }
                            }else if(index==1||index==2){
                              Navigator.of(context).push(
                                  CupertinoPageRoute(builder: (context){
                                    return YSAgreement(type: index);
                                  })
                              );
                            }
                          },
                          behavior: HitTestBehavior.opaque,
                          child: SizedBox(
                            height: hsp(45),
                            child: Row(
                              children: [
                                SizedBox(
                                  width: hsp(200),
                                  child: Text(item['title'],style: YSColors.contentStyle(context),),
                                ),
                                Expanded(
                                  child: Container(
                                    width: hsp(70),
                                    alignment: Alignment.centerRight,
                                    child: Text(item['value'],style: YSColors.subStyle(context),),
                                  ),
                                ),
                                Icon(Icons.chevron_right,size: hsp(20),color: YSColors.shadowColor(context),)
                              ],
                            ),
                          ),
                        );
                      },
                      separatorBuilder: (context,index){
                        return Divider(height: hsp(1),color: YSColors.lineColor(context),);
                      },
                      itemCount: _menuArray.length,
                      padding: EdgeInsets.only(left: hsp(15),right: hsp(10)),
                    ),
                  ),
                  Container(
                    width: hsp(280),
                    height: hsp(50),
                    alignment: Alignment.centerLeft,
                    child: Text(S.current.GUANFANGQUDAO,style: YSColors.contentStyle(context),),
                  ),
                  Container(
                    width: hsp(300),
                    decoration: BoxDecoration(
                      borderRadius: const BorderRadius.all(Radius.circular(10)),
                      color: YSColors.containColor(context),
                      boxShadow: [
                        BoxShadow(color: YSColors.lineColor(context),blurRadius: 3)
                      ],
                    ),
                    child: GridView.builder(
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 4,
                          mainAxisSpacing: hsp(20),
                          crossAxisSpacing: hsp(20)
                      ),
                      itemBuilder: (context,index){
                        Map item = _array[index];
                        return GestureDetector(
                          onTap: (){
                            int index = item['index'];
                            switch(index){
                              case 1:{

                              }break;
                              case 2:{}break;
                              case 3:{}break;
                              case 4:{}break;
                              case 5:{}break;
                              case 6:{}break;
                              case 7:{}break;
                            }
                          },
                          child: Image.asset(YSColors.imageStyle(context, item['icon'])),
                        );
                      },
                      itemCount: _array.length,
                      padding: EdgeInsets.all(hsp(20)),
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
