import 'package:file_preview/file_preview.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_tzh/gather/YSGather.dart';
import 'package:flutter_tzh/mine/YSMine.dart';
import 'package:flutter_tzh/tool/YSTools.dart';

import '../home/YSHome.dart';

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

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

class YSTabBarState extends State<YSTabBar> {
  int _selectedIndex = 0;
  final PageController _pageController = PageController();
  static const TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static const List<Widget> _widgetOptions = <Widget>[
    YSHome(),
    YSGather(),
    YSMine()
  ];
  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    _initTbs();
    super.initState();
  }

  void _onItemTapped(int index) {

  }
  _initTbs() async{
    bool hasInit = await FilePreview.tbsHasInit();
    if(hasInit)return;
    bool isInit = await FilePreview.initTBS(license: '123456');
    // FilePreview.tbsDebug();
    LogUtil.d('isInit============$isInit');
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF23262B),
      body:PageView.builder(
        controller: _pageController,
        itemBuilder: (context,index){
          return _widgetOptions[index];
        },
        itemCount: _widgetOptions.length,
        physics: const NeverScrollableScrollPhysics(),
      ),
      //_widgetOptions.elementAt(_selectedIndex),
      bottomNavigationBar: YSTabBarView(postIndex: (value){
        if(value==1){
          Navigator.of(context).push(
            CupertinoPageRoute(builder: (context){
               return const YSGather();
            })
          );
        }else{
          _pageController.jumpToPage(value);
          _selectedIndex = value;
          setState(() {});
        }
      },)
    );
  }
}


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

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

class YSTabBarViewState extends State<YSTabBarView> {
  final List<YSBarItem> _itemArray = [
    YSBarItem('images/home.png', 'images/home1.png', '首页',),
    YSBarItem('images/add.png', 'images/add.png', '',),
    YSBarItem('images/mine.png', 'images/mine1.png', '我的',),
  ];
  int _index = 0;
  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: const BorderRadius.all(Radius.circular(24)),
      child: Container(
        height: 114,
        color: const Color(0xFF2E3138),
        width: ysWidth(context),
        child: ListView.builder(itemBuilder: (context,index){
          YSBarItem item = _itemArray[index];
          return GestureDetector(
            onTap: (){
              if(item.label.isNotEmpty){
                _index = index;
                setState(() {});
              }
              widget.postIndex(index);
            },
            behavior: HitTestBehavior.opaque,
            child: Container(
              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: [
                  Image.asset(_index==index?item.imageSelected:item.image,height: 22,width: 22,),
                  Padding(
                    padding: const EdgeInsets.only(top: 5),
                    child: Text(item.label,style: TextStyle(fontSize: 11,color: _index==index?const Color(0xFF31D19E):Colors.white),),
                  )
                ],
              ),
            ),
          );
        },itemCount: _itemArray.length,scrollDirection: Axis.horizontal,),
      ),
    );
  }
}

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

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

}
