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';

///
///  优惠券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: CounponContainer(),
    // );
  }
}

class CounponContainer extends StatefulWidget {
  @override
  _CounponContainerState createState() => _CounponContainerState();
}

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

  @override
  Widget build(BuildContext context) {
    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();
        return Container();
      },
    );
  }

  /// 主视图
  Widget _getMainWdiget(CounponModel model) {
    return GestureDetector(
      onTap: () => _onJump(model),
      behavior: HitTestBehavior.opaque,
      child: Container(
        width: double.infinity,
        margin: const EdgeInsets.only(left: 12.5, right: 12.5),
        padding: const EdgeInsets.only(left: 18.5),
        alignment: Alignment.centerLeft,
        child: Row(
          children: <Widget>[
            /// 价格
            _getPriceWidget(model),
            const SizedBox(width: 7.5),
            /// 有效期
            _getTimeWidget(model)
          ],
        ),
      ),
    );
  }

  /// 价格
  Widget _getPriceWidget(CounponModel model) {
    return Row(
      children: <Widget>[
        /// 价格类型
        Text('￥', style: TextStyle(fontSize: 15, color: HexColor.fromHex('#FFFFFF'))),

        /// 价格
        Text('100', style: TextStyle(fontSize: 30, color: HexColor.fromHex('#FFFFFF'))),
      ],
    );
  }

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

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