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(),
    );
  }
}

class StoreContainer extends StatefulWidget {
  @override
  _StoreContainerState createState() => _StoreContainerState();
}

class _StoreContainerState extends State<StoreContainer> {
  /// 点击更多
  void _onMoreClick() {}

  /// 点击商家
  void _onStoreClick() {}

  @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 Container(
      color: Colors.white,
      width: double.infinity,
      height: 50 + 20.0 + 18,
      padding: const EdgeInsets.only(left: 12.5, right: 12.5, top: 20, bottom: 18),
      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?.shop_name_color ?? '#333333'), fontSize: 13, fontWeight: FontWeight.bold)),

        /// 更多
        Text(model?.more ?? '更多店铺优惠 >', 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?.description_color ?? '#999999', model?.description_leve_icon ?? ''),

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

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

  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 ?? '',

          ),
        ),
      ],
    );
  }
}
