import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/widgets/goods_details/tag/model/goods_details_tag_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class GoodsDetailsTagWidget extends StatelessWidget {
  final Map<String, dynamic> data;
  GoodsDetailsTagModel _model;

  GoodsDetailsTagWidget(this.data, {Key key}) : super(key: key) {
    try {
      _model = GoodsDetailsTagModel.fromJson(jsonDecode(data['data']));
    } catch(e, s) {
      Logger.error(e, s);
    }
  }

  @override
  Widget build(BuildContext context) {
    if(null == _model) return Container();
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(top: ParseUtil.stringParseDouble(_model?.topMargin), left: ParseUtil.stringParseDouble(_model?.leftRightMargin), right: ParseUtil.stringParseDouble(_model?.leftRightMargin)),
      padding: const EdgeInsets.only(left: 12.5, right: 12.5, top: 6, bottom: 6),
      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: _buildMainWidget(),
    );
  }

  Widget _buildMainWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        /// 领券立减100元 widget
        Visibility(visible: !EmptyUtil.isEmpty(_model?.couponText), child: _getCoumstonButtomWidget(_model?.couponText, _model?.tagList[1]?.textColor ?? '#FF4242', _model?.tagList[1]?.bgColor ?? '#FFE0E0')),

        /// 收货后返现5.5元
        Visibility(visible: !EmptyUtil.isEmpty(_model?.commissionText), child: _getCoumstonButtomWidget(_model?.commissionText, _model?.tagList[0]?.textColor ?? '#B78107', _model?.tagList[0]?.bgColor ?? '#FFEFDA')),
      ],
    );
  }


  Widget _getCoumstonButtomWidget(String text, String textColor, String bg) {
    return Container(
      margin: const EdgeInsets.only(right: 5),
      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),
      ),
    );
  }
}
