import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:zhiying_base_widget/pages/orders_page/models/order_model.dart';
import 'package:zhiying_base_widget/pages/orders_page/models/order_page_item_style_model.dart';
import 'package:zhiying_base_widget/pages/orders_page/models/order_page_style_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

class OrderItemWidget extends StatelessWidget {
  final OrderModel model;
  final OrderPageStyleModel style;

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

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 12.5, right: 12.5, top: 4, bottom: 4),
      padding: EdgeInsets.all(10),
      width: double.infinity,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(7.5), color: Colors.white),
      child: Column(
        children: <Widget>[
          _createHeader(),
          _createCenter(),
          _createTips(),
        ],
      ),
    );
  }

  Widget _createHeader() {
    List<OrderTypeModel> types = style.list.orderType;
    OrderTypeModel orderType = types.firstWhere((element) {
      return element.type == model.orderType.toString();
    });
    // orderType = types.last;
    String type = '';
    type = orderType.name ?? '';
    String replaceKey = orderType.replaceKey ?? '';
    if (replaceKey.length > 4) {
      String key = replaceKey.substring(2, replaceKey.length - 2);
      Logger.debug(key);
      if (model.data.containsKey(key)) {
        type = orderType.name.replaceAll(replaceKey, model.data[key]);
      }
    }

    List<OrderStateModel> states = style.list.orderState;
    OrderStateModel state =
        states.firstWhere((element) => element.type == model.state.toString());
    return Padding(
      padding: const EdgeInsets.only(bottom: 8),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Text(
              type ?? '',
              maxLines: 1,
              style: TextStyle(
                fontSize: 12,
                fontWeight: FontWeight.bold,
                color: Color(0xff333333),
              ),
            ),
          ),
          Text(
            state.name ?? '',
            maxLines: 1,
            style: TextStyle(
              fontSize: 12,
              fontWeight: FontWeight.bold,
              color: Color(0xff999999),
            ),
          ),
        ],
      ),
    );
  }

  Widget _createCenter() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(right: 10),
          width: 96,
          height: 96,
          child: ClipRRect(
            borderRadius: BorderRadius.circular(8),
            child: CachedNetworkImage(
              imageUrl: model.thumbnail ?? '',
              fit: BoxFit.cover,
            ),
          ),
        ),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              _createTitle(),
              Row(
                children: <Widget>[
                  _creteText(
                      '${style?.list?.textOrderNo ?? ''}${model?.ordId ?? ''}'),
                  GestureDetector(
                    onTap: () {
                      if (model?.ordId != null && model.ordId.length > 0) {
                        Fluttertoast.showToast(msg: '复制成功');
                        Clipboard.setData(ClipboardData(text: model.ordId));
                      }
                    },
                    child: Container(
                      margin: EdgeInsets.only(left: 10),
                      padding: EdgeInsets.only(
                        left: 10,
                        right: 10,
                      ),
                      decoration: BoxDecoration(
                          color: Color(0xfff5f5f5),
                          borderRadius: BorderRadius.circular(2.5),
                          border: Border.all(
                            color: Color(0xffd6d6d6),
                            width: 0.5,
                          )),
                      child: Text(
                        style.list.textCopy,
                        style: TextStyle(fontSize: 9, color: Color(0xff999999)),
                      ),
                    ),
                  )
                ],
              ),
              _creteText(
                  '${style?.list?.textOrderTime ?? ''}${model?.createAt ?? ''}'),
              _creteText(
                  '${style?.list?.textFinishTime ?? ''}${model?.confirmAt ?? ''}'),
            ],
          ),
        )
      ],
    );
  }

  Widget _createTitle() {
    List<InlineSpan> list = List();
    String shop = '';
    if (model?.provider != null && model.provider != '') {
      shop = style.filter.providerType
              .firstWhere((element) => element.type == model.provider)
              ?.name ??
          '';
    }
    if (shop != '') {
      list.add(WidgetSpan(
        child: Container(
          padding: EdgeInsets.only(left: 2, right: 2, top: 3, bottom: 3),
          margin: EdgeInsets.only(right: 4),
          child: Text(
            shop,
            style: TextStyle(
              fontSize: 9,
              height: 1,
              color:
                  HexColor.fromHex(style.list.colorProviderFont ?? '#ffffff'),
            ),
          ),
          decoration: BoxDecoration(
              color: HexColor.fromHex(style.list.colorProviderBg ?? '#ffffff'),
              borderRadius: BorderRadius.circular(2.5)),
        ),
      ));
    }
    list.add(
      TextSpan(
        text: model?.itemTitle ?? '',
        style: TextStyle(
            fontSize: 15,
            color: Color(0xff333333),
            fontWeight: FontWeight.bold),
      ),
    );
    return RichText(
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
      text: TextSpan(children: list),
    );
  }

  Widget _creteText(String text) {
    return text == null || text == ''
        ? Container()
        : Padding(
            padding: EdgeInsets.only(top: 2, bottom: 2),
            child: Text(
              text,
              style: TextStyle(
                fontSize: 10,
                color: Color(0xff999999),
              ),
            ),
          );
  }

  Widget _createTips() {
    List<OrderStateModel> states = style.list.orderState;
    OrderStateModel state =
        states.firstWhere((element) => element.type == model.state.toString());
    if (state == null || state.tips == null || state.tips == '') {
      return Container();
    }
    return Container(
      margin: EdgeInsets.only(top: 8),
      padding: EdgeInsets.only(left: 8, right: 8, top: 2, bottom: 2),
      width: double.infinity,
      decoration: BoxDecoration(
          color: HexColor.fromHex(style.list.colorTipsBg ?? '#f5f5f5'),
          borderRadius: BorderRadius.circular(4)),
      child: Text(
        state?.tips ?? '',
        style: TextStyle(color: Color(0xff666666), fontSize: 10),
      ),
    );
  }
}
