import 'package:flutter/material.dart';
import 'package:flutter_wallet/exchange/YSExchange.dart';
import 'package:flutter_wallet/find/YSFind.dart';
import 'package:flutter_wallet/setting/YSSetting.dart';
import 'package:flutter_wallet/shop/YSShop.dart';
import 'package:flutter_wallet/wallet/YSWallet.dart';
import 'package:provider/provider.dart';
import '../generated/l10n.dart';
import '../tools/YSAlertView.dart';
import '../tools/YSColors.dart';
import '../tools/YSNetWork.dart';
import '../tools/YSTools.dart';

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

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

class YSTabBarState extends State<YSTabBar> {
  final PageController _pageController = PageController();
  static const TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  int _index = 0;
  int _isRefresh = 0;
  static const List<Widget> _widgetOptions = <Widget>[
    YSWallet(),
    YSShop(),
    YSExchange(),
    YSFind(),
    YSSetting()
  ];
  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    _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){
        Map data = list.first;
        // PackageInfo packageInfo = await PackageInfo.fromPlatform();
        //int.parse(packageInfo.buildNumber)<data['build']&&data['is_force']==1;
        getVersionData(context,data);
      }
    });
  }


  @override
  Widget build(BuildContext context) {
    return NotificationListener<CustomerNotification>(
      onNotification: (object){
        _pageController.jumpToPage(2);
        _index = 2;
        _isRefresh++;
        setState(() {});
        return false;
      },
      child: Scaffold(
        backgroundColor: Colors.white,
        body:Consumer(
          builder: (context,CurrentLocale locale,child) {
            return PageView.builder(
              controller: _pageController,
              itemBuilder: (context,index){
                return _widgetOptions[index];
              },
              itemCount: _widgetOptions.length,
              physics: const NeverScrollableScrollPhysics(),
            );
          }
        ),
        //_widgetOptions.elementAt(_selectedIndex),
        bottomNavigationBar: YSTabBarView(postIndex: (value){
          _pageController.jumpToPage(value);
          setState(() {});
        },index: _index,key: Key('$_isRefresh'),)
      ),
    );
  }
}


class YSTabBarView extends StatefulWidget {
  final ValueSetter<int> postIndex;
  final int index;
  const YSTabBarView({Key? key, required this.postIndex, required this.index}) : super(key: key);

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

class YSTabBarViewState extends State<YSTabBarView> {
  int _index = 0;
  @override
  void initState() {
    _index = widget.index;
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    List<YSBarItem> _itemArray = [
      YSBarItem('编组 5', '编组 5', S.current.BAKEQIANBAO,),
      YSBarItem('导航-市场运营', '导航-市场运营', S.current.SHICHANG,),
      YSBarItem('编组 7', '编组 7', S.current.DUIHUAN,),
      YSBarItem('编组 8', '编组 8', S.current.FAXIAN,),
      YSBarItem('编组 9', '编组 9', S.current.SHEZHI,)
    ];
    return Container(
      decoration:  BoxDecoration(
        color: YSColors.containColor(context),
        boxShadow: const [
          BoxShadow(color: Colors.black12,blurRadius: 5,offset: Offset(0, -5))
        ]
      ),
      height: ystabBarHeight,
      width: ysWidth(context),
      child: ListView.builder(itemBuilder: (context,index){
        YSBarItem item = _itemArray[index];
        Color colorValue = _index==index?YSColors.iconColor(context):YSColors.unSelectedColor(context);
        return GestureDetector(
          onTap: (){
            if(item.label.isNotEmpty){
              _index = index;
              setState(() {});
            }
            widget.postIndex(index);
          },
          behavior: HitTestBehavior.opaque,
          child: Container(
            color: YSColors.tabColor(context),
            width: ysWidth(context)/_itemArray.length,
            alignment: Alignment.center,
            child: item.label.isEmpty?Image.asset(item.image,height: 60,width: 60,):Column(
              mainAxisSize: MainAxisSize.min,
              children: [//_index==index?item.imageSelected:
                Image.asset(YSColors.imageStyle(context, item.image),height: 22,width: 22,color: colorValue,),
                Padding(
                  padding: const EdgeInsets.only(top: 5),
                  child: Text(item.label, style: TextStyle(fontSize: 11,color: colorValue),),
                )
              ],
            ),
          ),
        );
      },itemCount: _itemArray.length,scrollDirection: Axis.horizontal,),
    );
  }
}

class YSBarItem{
  final String image;
  final String imageSelected;
  String label = '';

  YSBarItem(this.image, this.imageSelected, this.label);

}
