import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wallet/generated/l10n.dart';
import 'package:flutter_wallet/login/YSChooseIntoType.dart';
import 'package:flutter_wallet/login/YSWalletCreate.dart';
import 'package:flutter_wallet/login/YSWalletInto.dart';
import 'package:flutter_wallet/login/view/YSHelpView.dart';
import 'package:flutter_wallet/setting/YSAgreement.dart';
import 'package:flutter_wallet/tools/YSColors.dart';
import 'package:flutter_wallet/tools/YSNetWork.dart';

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

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

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

class YSWalletSettingState extends State<YSWalletSetting> {
  bool _selected = false;
  bool _isClose = false;
  final List _list = [
    {'title':'中文','type':'zh'},
    {'title':'English','type':'en'},
    {'title':'日本語','type':'ja'},
    {'title':'Русский','type':'ru'},
    {'title':'Français','type':'fr'},
    {'title':'한국인','type':'ko'}
  ];
  @override
  void initState() {
    YSData().isManager = false;
    _getVersionData();
    super.initState();
  }
  _getVersionData() async{
    YSNetWork.ysRequestHttp(context, type: RequestType.post, api: 'versions', parameter: {}, successSetter: (dict) async{
      List list = dict['data']['list'];
      setState(() {});
      if(list.isNotEmpty){
        Map data = list.first;
        getVersionData(context,data);
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: SizedBox(
          width: ysWidth(context),
          height: ysHeight(context),
          child: Stack(
            children: [
              const YSHeadView(),
              Positioned(
                right: hsp(15),
                top: ysTOP(context)+hsp(15),
                child: PopupMenuButton(
                  itemBuilder: (BuildContext context) {
                    return _list.map((e) => PopupMenuItem(value: e['type'],child: Text(e['title'],style: YSColors.contentStyle(context),),)).toList();
                    // return [
                    //   PopupMenuItem(value: "zh",child: Text("中文",style: YSColors.contentStyle(context),),),
                    //   PopupMenuItem(value: "en",child: Text("English",style: YSColors.contentStyle(context),),),
                    // ];
                  },
                  child: Text(S.current.YUYANSHEZHI,style: YSColors.content2Style(context),),
                  onSelected: (value){
                    setState(() {
                      YSUserRecord().language = '$value';
                    });
                    YSLanguage().changeLanguage(context, '$value');
                    YSSharedPreferences.setStringValue('language', '$value');
                  },
                )

              ),
              Positioned(
                left: ysWidth(context)/2-hsp(65),
                top: hsp(90),
                child: Image.asset(
                  YSColors.imageStyle(context, '资源 1'),
                  height: hsp(70),
                  width: hsp(130),
                )
              ),
              Positioned(
                  left: ysWidth(context)/2-hsp(125),
                  top: hsp(210),
                  child: Image.asset(
                    YSColors.imageStyle(context, '资源 3'),
                    height: hsp(30),
                    width: hsp(250),
                  )
              ),
              Positioned(
                  left: hsp(20),
                  top: hsp(320),
                  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(!_selected){
                                    ysFlutterToast(S.current.JIXUBUZHUOQINGXIANTONGYIYONGHUXIEYI);
                                    return;
                                  }
                                  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(20),);
                      },
                      itemCount: 2,
                      shrinkWrap: true,
                      padding: const EdgeInsets.all(0),
                      physics: const NeverScrollableScrollPhysics(),
                    ),
                  )
              ),
              Positioned(
                right: hsp(20),
                bottom: hsp(80),
                child: GestureDetector(
                  onTap: (){
                    _isClose = false;
                    setState(() {});
                  },
                  behavior: HitTestBehavior.opaque,
                  child: Image.asset(
                    YSColors.imageStyle(context, 'Agent'),
                    height: hsp(45),
                    width: hsp(45),
                  ),
                )
              ),
              if(!_isClose)Positioned(
                right: hsp(60),
                bottom: hsp(120),
                child: Container(
                  height: hsp(60),
                  width: hsp(130),
                  padding: EdgeInsets.all(hsp(10)),
                  decoration: BoxDecoration(
                    image: DecorationImage(image: AssetImage(YSColors.imageStyle(context, '气泡弹出框黑色样式1选项尖角右下')),fit: BoxFit.fill)
                  ),
                  alignment: Alignment.center,
                  child: Text(S.current.QIANBAOANQUANZHISHI,style: YSColors.content2Style(context),),
                )
              ),
              if(!_isClose)Positioned(
                  right: hsp(65),
                  bottom: hsp(162),
                  child: GestureDetector(
                    onTap: (){
                      _isClose = true;
                      setState(() {});
                    },
                    child: Image.asset(YSColors.imageStyle(context, '关闭'),height: hsp(15),width: hsp(15),),
                  )
              ),
              Positioned(
                bottom: hsp(20),
                height: hsp(30),
                left: hsp(50),
                child: Container(
                  alignment: Alignment.center,
                  width: ysWidth(context)-hsp(100),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      YSCheckView(valueSetter: (value){
                        _selected = value;
                      }),
                      SizedBox(
                        width: ysWidth(context)-hsp(120),
                        child: SingleChildScrollView(
                          scrollDirection: Axis.horizontal,
                          child: RichText(
                            text: TextSpan(
                              style: YSColors.subStyle(context),
                              children: [
                                TextSpan(text: ' ${S.current.JIXUBUZHUOBINGTONGYI}'),
                                TextSpan(
                                  text: '《${S.current.YINGHUXIEYI}》',
                                  style: const TextStyle(color: Color(0xFFED260C)),
                                  recognizer: TapGestureRecognizer()..onTap=(){
                                    Navigator.of(context).push(
                                      CupertinoPageRoute(builder: (context){
                                        return const YSAgreement(type: 2);
                                      })
                                    );
                                  }
                                ),
                              ]
                            )
                          ),
                        ),
                      )
                    ],
                  ),
                )
              ),
            ],
          ),
        ),
      ),
    );
  }
}
