package com.gingersoft.gsa.cloud.bean.mvp.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gingersoft.gsa.cloud.base.utils.LanguageUtils;
import com.gingersoft.gsa.cloud.base.utils.time.TimeUtils;
import com.gingersoft.gsa.cloud.bean.R;
import com.gingersoft.gsa.cloud.bean.R2;
import com.gingersoft.gsa.cloud.bean.mvp.model.bean.OrderDetailItem;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.qmuiteam.qmui.alpha.QMUIAlphaButton;

import java.util.Date;
import java.util.List;

import butterknife.BindView;

/**
 * 作者：ELEGANT_BIN
 * 版本：1.6.0
 * 创建日期：2018/6/27
 * 修订历史：2018/6/27
 * 描述：
 */
public class OrderCenterAdapter extends DefaultAdapter<OrderDetailItem> {

    private Context mContext;

    public OrderCenterAdapter(Context context, List<OrderDetailItem> infos) {
        super(infos);
        this.mContext = context;
    }

    @Override
    public int getItemCount() {
        return mInfos.size();
    }

    @Override
    public BaseHolder<OrderDetailItem> getHolder(View v, int viewType) {
        return new OrderCenterItemHolder(v);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_order_center;
    }

    class OrderCenterItemHolder extends BaseHolder<OrderDetailItem> {

        @BindView(R2.id.ll_root_container)
        LinearLayout ll_root_container;
        @BindView(R2.id.tv_table_name)
        TextView tv_table_name;
        @BindView(R2.id.tv_order_num)
        TextView tv_order_num;
        @BindView(R2.id.tv_people_num)
        TextView tv_people_num;
        @BindView(R2.id.tv_open_time)
        TextView tv_open_time;
        @BindView(R2.id.tv_pay_time)
        TextView tv_pay_time;
        @BindView(R2.id.tv_pay_money)
        TextView tv_pay_money;

        @BindView(R2.id.btn_print_order)
        QMUIAlphaButton btn_print_order;
        @BindView(R2.id.btn_tips)
        QMUIAlphaButton btn_tips;
        @BindView(R2.id.btn_refund)
        QMUIAlphaButton btn_refund;
        @BindView(R2.id.btn_modify_order)
        QMUIAlphaButton btn_modify_order;
        @BindView(R2.id.btn_cancel_order)
        QMUIAlphaButton btn_cancel_order;
        @BindView(R2.id.btn_order_status)
        QMUIAlphaButton btn_order_status;
        @BindView(R2.id.ll_bottom)
        LinearLayout ll_bottom;

        public OrderCenterItemHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData(OrderDetailItem item, int position) {

            String pax = LanguageUtils.get_language_system(mContext, "Meal.Mannumber", "人數") + "：";
            String openTable = LanguageUtils.get_language_system(mContext, "open.table", "開台") + "：";
            String payTable = LanguageUtils.get_language_system(mContext, "open.table", "結賬") + "：";
            String amount = LanguageUtils.get_language_system(mContext, "amount", "金額") + "：$";

            tv_people_num.setText(pax + item.getPerson());
            tv_open_time.setText(openTable + TimeUtils.getStringByFormat(new Date(item.getCreateTime()), TimeUtils.DEFAULT_DATE_FORMAT_YMDHM));
            if (!TextUtils.isEmpty(item.getEndTime())) {
                tv_pay_time.setText(payTable + TimeUtils.getStringByFormat(new Date(item.getEndTime()), TimeUtils.DEFAULT_DATE_FORMAT_YMDHM));
            }
            tv_pay_money.setText(amount + item.getTotalAmount());

            setTitleByOrderType(item);
            setOrderStatus(item, position);
            setBtnVisibilityByOrderType(item);

            ll_root_container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(item, position);
                    }
                }
            });
        }

        private void setTitleByOrderType(OrderDetailItem item) {
            String orderCode = LanguageUtils.get_language_system(mContext, "order.code", "訂單碼") + ": ";
            String mealCode = LanguageUtils.get_language_system(mContext, "meal.code", "取餐碼") + ": ";
            if (item.getOrderType() == 2) {
                tv_table_name.setText("外賣");
                tv_order_num.setText(orderCode + item.getTakeFoodCode());
            } else if (item.getOrderType() == 7) {
                tv_table_name.setText("自取");
                tv_order_num.setText(orderCode + item.getBillNo() + "  " + mealCode + item.getTakeFoodCode());
            } else {
                String tableName = LanguageUtils.get_language_system(mContext, "table.number", "檯號") + ": ";
                String orderNumber = LanguageUtils.get_language_system(mContext, "order.number", "訂單號") + ": ";
                tv_table_name.setText(tableName + item.getTableName());
                tv_order_num.setText(orderNumber + item.getId());
            }
        }

        private void setOrderStatus(OrderDetailItem item, int position) {
            btn_cancel_order.setVisibility(View.GONE);
            if (item.getStatus() == 6) {
                btn_print_order.setVisibility(View.GONE);
                btn_modify_order.setVisibility(View.GONE);
//                btn_cancel_order.setVisibility(View.GONE);
                btn_order_status.setVisibility(View.VISIBLE);
            } else {
                btn_print_order.setText(LanguageUtils.get_language_system(mContext, "print.order", "打印賬單"));
                btn_tips.setText(LanguageUtils.get_language_system(mContext, "public.tips", "貼士"));
                btn_modify_order.setText(LanguageUtils.get_language_system(mContext, "modify.order", "修改訂單"));
//                btn_cancel_order.setText(LanguageUtils.get_language_system(mContext, "cancel.order", "取消訂單"));
                btn_print_order.setVisibility(View.VISIBLE);
                btn_modify_order.setVisibility(View.VISIBLE);
//                btn_cancel_order.setVisibility(View.VISIBLE);
                btn_order_status.setVisibility(View.GONE);
                btn_print_order.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemClickListener != null) {
                            mOnItemClickListener.onPrintClick(item, position);
                        }
                    }
                });
                btn_modify_order.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemClickListener != null) {
                            mOnItemClickListener.onModifyClick(item, position);
                        }
                    }
                });
                btn_cancel_order.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemClickListener != null) {
                            mOnItemClickListener.onCancelClick(item, position);
                        }
                    }
                });
            }
        }

        private void setBtnVisibilityByOrderType(OrderDetailItem item) {
            if (item.getOrderType() == 2 || item.getOrderType() == 7) {
                //外賣自取 不可以修改訂單 取消訂單
                btn_modify_order.setVisibility(View.GONE);
                btn_cancel_order.setVisibility(View.GONE);
            }
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {

        void onItemClick(OrderDetailItem datasBean, int position);

        void onPrintClick(OrderDetailItem datasBean, int position);

        void onModifyClick(OrderDetailItem datasBean, int position);

        void onCancelClick(OrderDetailItem datasBean, int position);
    }
}
