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: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> {
  @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) {
          return ItemWidget(
            data: [1, 2, 3],
          );
        }
        if (state is HomeBannerCacheState) {
          return ItemWidget(
            data: [1, 2],
          );
        }

        return HomeBannerSkeleton(widget.model);
      },
    );
  }
}

class ItemWidget extends StatelessWidget {
  final List data;

  ItemWidget({this.data});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 180.h,
      margin: EdgeInsets.only(top: 7.5, left: 2.5, right: 2.5),
      child: Row(
        children: data.map((index) {
          return Flexible(
            flex: 1,
            child: Container(
              margin: EdgeInsets.only(left: 5, right: 5),
              decoration: BoxDecoration(
                  color: Colors.lightBlue,
                  borderRadius: BorderRadius.circular(7.5)),
            ),
          );
        }).toList(),
      ),
    );
  }
}
