import 'dart:convert' as convert;

import 'package:event_bus/event_bus.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/bloc/home_goods_header_bloc.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_creater.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_header_model.dart';
import 'package:zhiying_base_widget/widgets/home/home_goods/models/home_goods_style_model.dart';
import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class HomeGoodsHeaderDelegate extends SliverPersistentHeaderDelegate {
  final Widget child;

  HomeGoodsHeaderDelegate(this.child) : super();

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    print('${shrinkOffset.toString()}  ${overlapsContent.toString()}');
    return child;
  }

  @override
  double get maxExtent => 64.0;

  @override
  double get minExtent => 64.0;

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) =>
      false; // 如果内容需要更新，设置为true
}

class HomeGoodsHeader extends StatelessWidget {
  final Map<String, dynamic> model;
  final EventBus eventBus;

  const HomeGoodsHeader(
    this.model,
    this.eventBus, {
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider<HomeGoodsHeaderBloc>(
      bloc: HomeGoodsHeaderBloc(),
      child: _HomeGoodsHeader(model, eventBus),
    );
  }
}

class _HomeGoodsHeader extends StatefulWidget {
  final Map<String, dynamic> model;
  final EventBus eventBus;

  _HomeGoodsHeader(this.model, this.eventBus, {Key key}) : super(key: key);

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

class _HomeGoodsHeaderState extends State<_HomeGoodsHeader>
    with TickerProviderStateMixin {
  HomeGoodsStyleModel _style;
  List<Widget> _widgets = List();

  TabController _tabController;
  int _currentIndex = 0;

  @override
  void initState() {
    String d = widget.model['data'];
    dynamic json = convert.jsonDecode(d);
    _style = HomeGoodsStyleModel.fromJson(Map<String, dynamic>.from(json));

    _tabController =
        TabController(length: _style.recommendList.length, vsync: this);
    if (_style.recommendList.first != null) {
      widget.eventBus
          .fire(HomeGoodsHeaderEvent(_style.recommendList.first.type));
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _widgets.clear();
    for (int index = 0; index < _style.recommendList.length; index++) {
      _widgets.add(_HomeGoodsHeaderItem(
        _style.recommendList[index],
        isSelected: index == _currentIndex,
      ));
    }

    return Container(
      width: double.infinity,
      height: double.infinity,
      color: Color(0xfff9f9f9),
      child: TabBar(
        indicatorColor: Colors.transparent,
        controller: _tabController,
        isScrollable: true,
        tabs: _widgets,
        onTap: (index) {
          widget.eventBus
              .fire(HomeGoodsHeaderEvent(_style.recommendList[index].type));
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}

class _HomeGoodsHeaderItem extends StatelessWidget {
  final bool isSelected;
  final HomeGoodsHeaderModel model;

  const _HomeGoodsHeaderItem(this.model, {Key key, this.isSelected = false})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        // width: 60,
        // height: 54,
        child: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            model.title,
            style: TextStyle(
                color: HexColor.fromHex(model.tilteTextColor),
                fontSize: 17,
                fontWeight: FontWeight.bold),
          ),
          Container(
            margin: EdgeInsets.only(top: 4),
            padding: EdgeInsets.only(left: 6, right: 6, top: 2, bottom: 2),
            decoration: BoxDecoration(
                color: isSelected
                    ? HexColor.fromHex(model.subtitleBgColor)
                    : Colors.transparent,
                borderRadius: BorderRadius.circular(18)),
            child: Text(
              model.subtitle,
              style: TextStyle(
                  fontSize: 10,
                  color: isSelected
                      ? HexColor.fromHex(model.subtitleTextColor)
                      : Color(0xff999999)),
              textAlign: TextAlign.center,
            ),
          ),
        ],
      ),
    ));
  }
}
