import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/coupon.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/view_widget/separator.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class NewCouponWidget extends StatelessWidget {
  final Function(int type) callback;
  final GestureTapCallback callbackEx;
  final Coupon coupon;
  final int type;

  NewCouponWidget(this.coupon, this.callback, this.callbackEx, {this.type = 1});

  @override
  Widget build(BuildContext context) {
    return buildCoupon(context);
  }

  Widget buildCoupon(BuildContext context) {
    // if(coupon != null) {
    //   coupon.isEx = fa;
    // }

    // if (coupon != null) {
    //   print("object: ${coupon.toJson()}");
    // }

    return Container(
      height: (coupon != null && coupon.isEx) ? 152.h : 140.h,
      width: double.infinity,
      margin: EdgeInsets.fromLTRB(14.w, 6.h, 14.w, 6.h),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withAlpha(12),
            offset: Offset(0, 3),
            blurRadius: 14,
            spreadRadius: 0,
          )
        ],
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: Container(
              margin: EdgeInsets.only(top: 14.h, bottom: 8.h),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(
                    child: Container(
                      child: priceWidget(context),
                    ),
                    flex: 1,
                  ),
                  Expanded(
                    flex: 2,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Container(
                              padding: EdgeInsets.fromLTRB(4, 2, 4, 2),
                              decoration: BoxDecoration(
                                borderRadius:
                                    BorderRadius.circular(2),
                                border: Border.all(
                                  width: 1,
                                  color: Color(0xFFFF7A1A),
                                  style: BorderStyle.solid,
                                ),
                              ),
                              child: Text(
                                S.of(context).shangjiaquan,
                                style: TextStyle(
                                  fontSize: 10.sp,
                                  fontWeight: MyFontWeight.medium,
                                  color: Color(0xFFFF7A1A),
                                ),
                              ),
                            ),
                            SizedBox(
                              width: 8.w,
                            ),
                            Expanded(
                              child: Text(
                                (coupon.storeId != "0" && coupon?.storeList != null && coupon.storeList.firstWhere((x) => x.id == coupon.storeId)!=null ?
                                coupon.storeList.firstWhere((x) => x.id == coupon.storeId).storeName : coupon.tenantName),
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                  fontSize: 16.sp,
                                  fontWeight: MyFontWeight.semi_bold,
                                  color: Color(0xff353535),
                                ),
                              ),
                              flex: 1,
                            )
                          ],
                        ),
                        Text(
                          coupon != null ? coupon.couponName ?? "" : "",
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            fontSize: 14.sp,
                            color: Color(0xff727272),
                          ),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.end,
                          children: [
                            Visibility(
                              child: Text(
                                S.of(context).quanchangtongyong,
                                style: TextStyle(
                                  color: Color(0xFF353535),
                                  fontSize: 12.sp,
                                ),
                              ),
                              visible:
                                  (coupon != null && (coupon.bizType == 1)) ??
                                      false,
                            ),
                            Container(
                              margin: EdgeInsets.only(right: 23.w),
                              child: rightBtn(context),
                            )
                          ],
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            flex: (coupon != null && coupon.isEx) ? 97 : 97,
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 23.w),
            child: MySeparator(
              width: 5.w,
              height: 1.h,
              color: Color(0xFF353535),
            ),
          ),
          Expanded(
            flex: (coupon != null && coupon.isEx) ? 56 : 42,
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 23.w, vertical: 8.h),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Text(
                        S.of(context).xiangqing,
                        style: TextStyle(
                          color: Color(0xFF353535),
                          fontSize: 10.sp,
                        ),
                      ),
                      GestureDetector(
                        child: Icon(
                          (coupon != null && !coupon.isEx)
                              ? Icons.keyboard_arrow_down
                              : Icons.keyboard_arrow_up,
                          color: Colors.black,
                          size: 18,
                        ), //点击按钮布局
                        onTap: callbackEx,
                      ),
                    ],
                  ),
                  if (coupon != null && coupon.isEx)
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          S.of(context).shiyongriqi,
                          style: TextStyle(
                            color: Color(0xFF353535),
                            fontSize: 10.sp,
                          ),
                        ),
                        Text(
                          (coupon.useStartTime == null &&
                                  coupon.useEndTime == null)
                              ? S.of(context).quantian
                              : "${coupon.useStartTime.replaceAll("-", ".").split(" ")[0]} - ${coupon.useEndTime.replaceAll("-", ".").split(" ")[0]}",
                          style: TextStyle(
                            color: Color(0xFF353535),
                            fontSize: 10.sp,
                          ),
                        ),
                      ],
                    ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget priceWidget(BuildContext context) {
    if (coupon.bizType == 1) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.baseline,
              textBaseline: TextBaseline.alphabetic,
              children: [
                Text(
                  "¥",
                  style: TextStyle(
                    fontSize: 22.sp,
                    fontWeight: MyFontWeight.medium,
                    color: Color(0xff32A060),
                  ),
                ),
                SizedBox(
                  height: 4.h,
                ),
                Text(
                  coupon != null
                      ? double.tryParse("${coupon.discountAmount}" ?? "0")
                          .toInt()
                          .toString()
                      : "",
                  style: TextStyle(
                    fontSize: 40.sp,
                    fontWeight: MyFontWeight.semi_bold,
                    color: Color(0xff32A060),
                  ),
                ),
              ],
            ),
          ),
          Text(
            S.of(context).manyuankeyong(coupon != null
                ? ("${coupon.fullAmount}" ?? "0")
                    .toString()
                : ""),
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: MyFontWeight.semi_bold,
              color: Color(0xff32A060),
            ),
          ),
        ],
      );
    } else if (coupon.bizType == 3) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  S.of(context).duihuanquan,
                  style: TextStyle(
                    fontSize: 25.sp,
                    fontWeight: MyFontWeight.semi_bold,
                    color: Color(0xff32A060),
                  ),
                ),
              ],
            ),
          ),
          Text(
            S.of(context).xianshangshiyong,
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: MyFontWeight.semi_bold,
              color: Color(0xff32A060),
            ),
          ),
        ],
      );
    } else if (coupon.bizType == 5) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  S.of(context).duihuanquan,
                  style: TextStyle(
                    fontSize: 25.sp,
                    fontWeight: MyFontWeight.semi_bold,
                    color: Color(0xff32A060),
                  ),
                ),
              ],
            ),
          ),
          Text(
            S.of(context).xianxiashiyong,
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: MyFontWeight.semi_bold,
              color: Color(0xff32A060),
            ),
          ),
        ],
      );
    } else {
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.baseline,
              textBaseline: TextBaseline.alphabetic,
              children: [
                Text(
                  coupon != null
                      ? "${(coupon.discountPercent / 10.0 >= 10) ? 10 : coupon.discountPercent / 10.0}" ??
                          "0"
                      : "",
                  style: TextStyle(
                    fontSize: 40.sp,
                    fontWeight: MyFontWeight.semi_bold,
                    color: Color(0xff32A060),
                  ),
                ),
                SizedBox(
                  height: 4.h,
                ),
                Text(
                  "折",
                  style: TextStyle(
                    fontSize: 20.sp,
                    fontWeight: MyFontWeight.semi_bold,
                    color: Color(0xff32A060),
                  ),
                ),
              ],
            ),
          ),
          Text(
            S.of(context).quanchangtongyong,
            style: TextStyle(
              fontSize: 12.sp,
              fontWeight: MyFontWeight.semi_bold,
              color: Color(0xff32A060),
            ),
          ),
        ],
      );
    }
  }

  Widget rightBtn(context) {
    if (type == 1) {
      if (coupon != null && coupon.status == 0) {
        return Align(
          alignment: Alignment.centerRight,
          child: InkWell(
            onTap: () {
              callback(type);
            },
            child: Container(
              padding: EdgeInsets.fromLTRB(16.w, 2.h, 16.w, 2.h),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.5),
                color: Color(0xFF32A060),
              ),
              child: Text(
                S.of(context).lingqu,
                style: TextStyle(
                  fontSize: 12.sp,
                  fontWeight: MyFontWeight.medium,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        );
      } else {
        return Align(
          alignment: Alignment.centerRight,
          child: Container(
            padding: EdgeInsets.fromLTRB(16.w, 2.h, 16.w, 2.h),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10.5),
              color: Colors.grey,
            ),
            child: Text(
              S.of(context).yilingqu,
              style: TextStyle(
                fontSize: 12.sp,
                fontWeight: MyFontWeight.medium,
                color: Colors.white,
              ),
            ),
          ),
        );
      }
    } else {
      if (coupon != null && coupon.status == 1) {
        return Align(
          alignment: Alignment.centerRight,
          child: InkWell(
            onTap: () {
              callback(type);
            },
            child: Container(
              padding: EdgeInsets.fromLTRB(16.w, 2.h, 16.w, 2.h),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.5),
                color: Color(0xFF32A060),
              ),
              child: Text(
                (coupon.bizType == 5 || coupon.bizType == 3)
                    ? S.of(context).quhexiao
                    : S.of(context).qushiyong,
                style: TextStyle(
                  fontSize: 12.sp,
                  fontWeight: MyFontWeight.medium,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        );
      } else if (coupon != null && coupon.status == 2) {
        return Align(
          alignment: Alignment.centerRight,
          child: Container(
            padding: EdgeInsets.fromLTRB(16.w, 2.h, 16.w, 2.h),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10.5),
              // border: Border.all(
              //     width: 1, color: Color(0xFFA0A0A0), style: BorderStyle.solid)
              color: Color(0xFFA0A0A0),
            ),
            child: Text(
              S.of(context).yishiyong,
              style: TextStyle(
                fontSize: 12.sp,
                fontWeight: MyFontWeight.medium,
                color: Colors.white,
              ),
            ),
          ),
        );
      } else {
        return Align(
          alignment: Alignment.centerRight,
          child: Container(
            padding: EdgeInsets.fromLTRB(16.w, 2.h, 16.w, 2.h),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10.5),
              // border: Border.all(
              //     width: 1, color: Color(0xFFA0A0A0), style: BorderStyle.solid)
              color: Color(0xFFA0A0A0),
            ),
            child: Text(
              S.of(context).yishixiao,
              style: TextStyle(
                fontSize: 12.sp,
                fontWeight: MyFontWeight.medium,
                color: Colors.white,
              ),
            ),
          ),
        );
      }
    }
  }
}