import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:zhiying_base_widget/widgets/home/home_banner/bloc/bloc.dart';
import 'package:zhiying_base_widget/widgets/home/home_banner/bloc/home_banner_repository.dart';
import 'package:zhiying_base_widget/widgets/home/home_banner/home_banner_sk.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:zhiying_base_widget/widgets/home/home_banner/model/HomeBannerModel.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

///
/// 不可以滚动Banner
///
class HomeBannerWidget extends StatefulWidget {
  final Map<String, dynamic> model;

  const HomeBannerWidget(this.model);

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

class _HomeBannerWidgetState extends State<HomeBannerWidget> {
  @override
  Widget build(BuildContext context) {
    return BlocProvider<HomeBannerBloc>(
      create: (_) => HomeBannerBloc(repository: HomeBannerRepository())..add(HomeBannerInitEvent(widget.model)),
      child: HomeBannerContainer(
        model: widget.model,
      ),
    );
  }
}

class HomeBannerContainer extends StatefulWidget {
  final Map<String, dynamic> model;

  const HomeBannerContainer({@required this.model});

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

class _HomeBannerContainerState extends State<HomeBannerContainer> {

  /// 点击事件
  void _itemOnClick(HomeBannerListItemModel model){
    print('${model?.skip_identifier}');
    RouterUtil.route(model.toJson(), context);
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<HomeBannerBloc, HomeBannerState>(
      listener: (context, state) {},
      buildWhen: (previous, current) {
        if (current is HomeBannerErrorState) {
          return false;
        }
        return true;
      },
      builder: (context, state) {
        print(state);
        if (state is HomeBannerLoadedState) {
          int lenght = state?.model?.list?.length ?? 0;
          if(lenght > 0)
            return _getMainWidget(data: state?.model?.list);
          else
            return HomeBannerSkeleton(widget?.model);
        }
        if (state is HomeBannerCacheState) {
          int lenght = state?.model?.list?.length ?? 0;
          if(lenght > 0)
            return _getMainWidget(data: state?.model?.list);
          else
            return HomeBannerSkeleton(widget?.model);
        }
        return HomeBannerSkeleton(widget.model);
      },
    );
  }

  /// 获取主要视图
  Widget _getMainWidget({List<HomeBannerListItemModel> data}){
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(top: 7.5, left: 2.5, right: 2.5),
      child: Row(
        children: data.map((model) {
          return Flexible(
            flex: 1,
            child: GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: ()=> _itemOnClick(model),
              child: Container(
                margin: EdgeInsets.only(left: 5, right: 5),
                // decoration: BoxDecoration(color: Colors.lightBlue, borderRadius: BorderRadius.circular(7.5)),
                child: CachedNetworkImage(imageUrl: model?.img),
              ),
            ),
          );
        }).toList(),
      ),
    );
  }

}
//
// class ItemWidget extends StatelessWidget {
//   final List<HomeBannerListItemModel> data;
//
//   ItemWidget({this.data});
//
//   @override
//   Widget build(BuildContext context) {
//
//   }
// }
