import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/goods_details/store/bloc/bloc.dart';
import 'package:zhiying_base_widget/widgets/goods_details/store/bloc/store_repository.dart';
import 'package:zhiying_base_widget/widgets/goods_details/store/model/store_model.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:zhiying_base_widget/widgets/home/home_quick_entry/cached_network_image_util.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'store_sk.dart';

///
/// 商家widget
///
class StoreWidget extends StatelessWidget {
  final Map<String, dynamic> model;

  const StoreWidget(this.model);

  @override
  Widget build(BuildContext context) {
    return BlocProvider<StoreBloc>(
      create: (_) => StoreBloc(repository: StoreRepository()),
      //..add(StoreInitEvent(model: model)),
      child: StoreContainer(
        model,
        key: UniqueKey(),
      ),
    );
  }
}

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

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

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

class _StoreContainerState extends State<StoreContainer> {
  @override
  void initState() {
    BlocProvider.of<StoreBloc>(context).add(StoreInitEvent(model: widget?.model));
    super.initState();
  }

  /// 点击更多
  void _onMoreClick() {}

  /// 点击商家
  void _onStoreClick(StoreModel model) {
    print("点击商家_onStoreClick");
    Logger.log(widget.model['data']);
    Logger.log("店铺：" + model.shop_url);
    if (!EmptyUtil.isEmpty(model.shop_url)) {
      RouterUtil.openWebview(model.shop_url, context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<StoreBloc, StoreState>(
      listener: (context, state) {},
      buildWhen: (prev, current) {
        if (current is StoreErrorState) {
          return false;
        }
        return true;
      },
      builder: (context, state) {
        if (state is StoreLoadedState) {
          return _getMianWidget(state.model);
        }
        return StoreSkeleton();
      },
    );
  }

  /// 主视图
  Widget _getMianWidget(StoreModel model) {
    return GestureDetector(
      onTap: () {
        _onStoreClick(model);
      },
      child: Container(
        width: double.infinity,
        height: 50 + 20.0 + 18,
        padding: const EdgeInsets.only(left: 12.5, right: 12.5, top: 20, bottom: 18),
        margin: EdgeInsets.only(
            top: ParseUtil.stringParseDouble(model?.topMargin),
            left: ParseUtil.stringParseDouble(model?.leftRightMargin),
            right: ParseUtil.stringParseDouble(model?.leftRightMargin)),
        decoration: BoxDecoration(
            color: HexColor.fromHex(model?.bgColor),
            borderRadius: BorderRadius.only(
              topRight:
                  Radius.circular(ParseUtil.stringParseDouble(model?.topRightRadius)),
              topLeft: Radius.circular(ParseUtil.stringParseDouble(model?.topLeftRadius)),
              bottomRight:
                  Radius.circular(ParseUtil.stringParseDouble(model?.bottomRightRadius)),
              bottomLeft:
                  Radius.circular(ParseUtil.stringParseDouble(model?.bottomLeftRadius)),
            )),
        child: Row(
          children: <Widget>[
            /// 商家图片
            _getStoreImgWidget(model),
            const SizedBox(width: 7.5),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  /// 商店名称与更多
                  _getStoreNameWidget(model),

                  /// 评分描述
                  _getEvaluateWidget(model),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 商家图片
  Widget _getStoreImgWidget(StoreModel model) {
    return Container(
      width: 50,
      height: 50,
      // color: Colors.red,
      child: CachedNetworkImage(
        imageUrl: model?.shop_avatar ?? '',
        fit: BoxFit.fill,
      ),
    );
  }

  /// 商店名称
  Widget _getStoreNameWidget(StoreModel model) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        /// 商店名称
        Text(model?.shop_name ?? '',
            style: TextStyle(
                color: HexColor.fromHex(model?.shopTextColor ?? '#333333'),
                fontSize: 13,
                fontWeight: FontWeight.bold)),

        /// 更多 TODO 暂时隐藏，等后台有数据后再打开
        Visibility(
            visible: false,
            child: Text(model?.moreText ?? '更多店铺优惠',
                style: TextStyle(color: HexColor.fromHex('#FF4242'), fontSize: 11))),
      ],
    );
  }

  /// 评分描述
  Widget _getEvaluateWidget(StoreModel model) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        /// 宝贝描述 5.0
        _getCoustomWidet(model?.description ?? '宝贝描述 5.0',
            model?.descTextColor1 ?? '#999999', model?.heightPointIcon ?? ''),

        /// 物流服务 5.0
        _getCoustomWidet(model?.logistics ?? '宝贝描述 5.0',
            model?.descTextColor2 ?? '#999999', model?.publicPointIcon ?? ''),

        /// 服务态度 1.0
        _getCoustomWidet(model?.service ?? '宝贝描述 5.0', model?.descTextColor3 ?? '#999999',
            model?.lowPointIcon ?? ''),
      ],
    );
  }

  Widget _getCoustomWidet(String text, String textColor, String icon) {
    return Row(
      children: <Widget>[
        Text(text, style: TextStyle(fontSize: 11, color: HexColor.fromHex(textColor))),
        const SizedBox(width: 3),
        Container(
          width: 12,
          height: 12,
          // color: Colors.red,
          child: CachedNetworkImage(
            imageUrl: icon ?? '',
          ),
        ),
      ],
    );
  }
}
