import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/goods_details/title/model/goods_details_title_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

///
/// 商品详情标题
///
class GoodsDetailsTitleWidget extends StatelessWidget {
  final Map<String, dynamic> model;
  GoodsDetailsTitleModel _model;

  GoodsDetailsTitleWidget(this.model, {Key key}) : super(key: key) {
    try {
      _model = GoodsDetailsTitleModel.fromJson(jsonDecode(model['data']));
    } catch (e) {
      Logger.log('GoodsDetailsTitleWidget e = $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      color: Colors.white,
      padding: const EdgeInsets.only(left: 12.5, right: 12.5, top: 6.5),
      child: _getMaiWidget(_model),
    );
  }

  /// 主widget
  Widget _getMaiWidget(GoodsDetailsTitleModel model) {
    return Column(
      children: <Widget>[
        RichText(
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
          text: TextSpan(children: [
            WidgetSpan(
              child: _getGoodsTypeIcon(model),
            ),
            _getGoodsTitle(model),
          ]),
        ),
        /// 优惠卷说明
        Padding(padding: const EdgeInsets.only(top: 12.5), child: _getCounponDestrWidget(model)),
      ],
    );
  }

  /// 商品类型图标
  Widget _getGoodsTypeIcon(GoodsDetailsTitleModel model) {
    return Container(
      // height: 15,
      // width: 32,
      padding: const EdgeInsets.only(left: 7.5, right: 7.5, bottom: 2, top: 3),
      margin: const EdgeInsets.only(right: 4),
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(2.5), color: HexColor.fromHex(model?.provider_bg_color)),
      child: Text(model?.provider_name, style: TextStyle(color: HexColor.fromHex(model?.provider_name_color), fontSize: 9)),
    );
  }

  /// 商品标题
  InlineSpan _getGoodsTitle(GoodsDetailsTitleModel model) {
    return TextSpan(
        text: model?.title ?? '品胜（PISEN）苹果数据线1.5米 适用于苹果手机所有机型 MFI认证安全稳定一年换新1.5米2米',
        style: TextStyle(
          fontWeight: FontWeight.bold,
          fontSize: 14,
          color: HexColor.fromHex(model?.good_title_color ?? '#FF333333'),
        ));
  }

  /// 优惠卷说明
  Widget _getCounponDestrWidget(GoodsDetailsTitleModel model) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        /// 领券立减100元 widget
        _getCoumstonButtomWidget(model?.coupon_text, model?.coupon_text_color, model?.coupon_bg_color),
        const SizedBox(width: 5),

        /// 收货后返现5.5元
        _getCoumstonButtomWidget(model?.commission_text, model?.commission_text_color, model?.commission_bg_color),
      ],
    );
  }

  Widget _getCoumstonButtomWidget(String text, String textColor, String bg) {
    return Container(
      padding: const EdgeInsets.only(left: 13.5, right: 14.5, top: 2.5, bottom: 2.5),
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(30), color: HexColor.fromHex(bg)),
      child: Text(
        text,
        style: TextStyle(color: HexColor.fromHex(textColor), fontSize: 11),
      ),
    );
  }
}
