import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/goods_details/coupon/bloc/bloc.dart';
import 'package:zhiying_base_widget/widgets/goods_details/coupon/bloc/counpon_repository.dart';
import 'package:zhiying_base_widget/widgets/goods_details/coupon/counpon_sk.dart';
import 'package:zhiying_base_widget/widgets/goods_details/coupon/model/counpon_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:cached_network_image/cached_network_image.dart';

///
///  优惠券widget
///
class CounponWidget extends StatelessWidget {
  final Map<String, dynamic> model;

  const CounponWidget(this.model);

  @override
  Widget build(BuildContext context) {
    // return Container();
    return BlocProvider<CounponBloc>(
      create: (_) => CounponBloc(repository: CounponRepository())..add(CounponInitEvent(model: model)),
      child: CounponWidgetContainer(),
    );
  }
}

class CounponWidgetContainer extends StatefulWidget {
  @override
  _CounponWidgetContainerState createState() => _CounponWidgetContainerState();
}

class _CounponWidgetContainerState extends State<CounponWidgetContainer> {
  /// 点击领取
  void _onJump(CounponModel model) {}

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<CounponBloc, CounponState>(
      listener: (context, state) {},
      buildWhen: (prev, current) {
        if (current is CounponErrorState) {
          return false;
        }
        return true;
      },
      builder: (context, state) {
        if (state is CounponLoadedState) {
          return _getMainWdiget(state.model);
        }
        return CounponSkeleton();
      },
    );
  }

  /// 主视图
  Widget _getMainWdiget(CounponModel model) {
    return Visibility(
      visible: !EmptyUtil.isEmpty(model?.coupon_price),
      child: GestureDetector(
        onTap: () => _onJump(model),
        behavior: HitTestBehavior.opaque,
        child: Container(
          width: double.infinity,
          color: Colors.white,
          padding: const EdgeInsets.only(left: 12.5, right: 12.5, top: 12),
          child: Container(
            // color: Colors.red,
            width: double.infinity,
            padding: const EdgeInsets.only(left: 18.5, top: 12, bottom: 14),
            decoration: BoxDecoration(
              image: DecorationImage(
                image: CachedNetworkImageProvider(
                  model?.bg_img ??'',
                ),
                fit: BoxFit.fill
              )
            ),
            alignment: Alignment.centerLeft,
            child: Row(
              children: <Widget>[
                /// 价格
                _getPriceWidget(model),
                const SizedBox(width: 7.5),
                /// 有效期
                _getTimeWidget(model)
              ],
            ),
          ),
        ),
      ),
    );
  }

  /// 价格
  Widget _getPriceWidget(CounponModel model) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        /// 价格类型
        Text(model?.price_type ?? '¥ ', style: TextStyle(fontSize: 15, color: HexColor.fromHex(model?.price_type_color ?? '#FFFFFF'))),

        /// 价格
        Text(model?.coupon_price ??'100', style: TextStyle(fontSize: 30, color: HexColor.fromHex(model?.coupon_price_color ?? '#FFFFFF'), fontFamily: 'Din', package: 'zhiying_base_widget')),
      ],
    );
  }

  /// 名称与有效期
  Widget _getTimeWidget(CounponModel model) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        /// 标题
        Text(model?.coupon_title ?? '优惠券', style: TextStyle(fontSize: 17, color: HexColor.fromHex( model?.coupon_title_color ?? '#FFFFFF'))),

        /// 到期时间
        Text(model?.coupon_endtime ?? '有效期至2020-10-01', style: TextStyle(fontSize: 10, color: HexColor.fromHex(model?.coupon_time_color ?? '#FFFFFF')))
      ],
    );
  }
}
