package com.gingersoft.gsa.cloud.table.mvp.ui.adapter.meal;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gingersoft.gsa.cloud.app.GsaCloudApplication;
import com.gingersoft.gsa.cloud.database.bean.ColorBean;
import com.gingersoft.gsa.cloud.database.bean.Modifier;
import com.gingersoft.gsa.cloud.table.R;
import com.gingersoft.gsa.cloud.table.R2;
import com.gingersoft.gsa.cloud.ui.view.BeveLabelView;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.utils.ArmsUtils;

import java.util.List;

import butterknife.BindView;

/**
 * 作者：ELEGANT_BIN
 * 版本：1.6.0
 * 创建日期：2018/6/27
 * 修订历史：2018/6/27
 * 描述：
 */

public class ModifierAdapter extends DefaultAdapter<Modifier> {

    private Context mContext;

    private int ColHeight;
    private int FontSize;

    private int soldoutModFlagHeight;
    private int OrderNumberChildFontSize;


    public ModifierAdapter(Context context, List<Modifier> infos) {
        super(infos);
        this.mContext = context;

        ColHeight = GsaCloudApplication.uiStyleConfiguration.getModBtnHeightValue();
        FontSize = GsaCloudApplication.uiStyleConfiguration.getModFontSizeValue();
        soldoutModFlagHeight = GsaCloudApplication.uiStyleConfiguration.getSoldoutModFlagHeightValue();
        OrderNumberChildFontSize = GsaCloudApplication.uiStyleConfiguration.getSoldoutModFlagFontSizeValue();
    }

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

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

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

    class ComboItemHolder extends BaseHolder<Modifier> {

        @BindView(R2.id.tv_name)
        TextView tv_name;
        @BindView(R2.id.tv_soldout)
        TextView tv_soldout;

        @BindView(R2.id.iv_qty_sold)
        ImageView iv_qty_sold;
        @BindView(R2.id.iv_return)
        ImageView iv_return;
        @BindView(R2.id.blv_soldout)
        BeveLabelView blv_soldout;

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

        @Override
        public void setData(Modifier datasBean, int position) {

            iv_qty_sold.setVisibility(View.INVISIBLE);
            tv_soldout.setVisibility(View.INVISIBLE);

            initModifierName(datasBean);
            initModifierSoldout(datasBean);
            initModifierLayoutParams();
            tv_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(datasBean, position);
                    }
                }
            });
        }

        private void initModifierName(Modifier datasBean) {
            String desc = datasBean.getModifierName();
            if (datasBean.getBgColor() != -1) {
                tv_name.setBackgroundColor(datasBean.getBgColor());
                tv_name.setTextColor(datasBean.getFontColor());
            } else {
                tv_name.setBackgroundColor(ArmsUtils.getColor(mContext, R.color.keyboard_paybill_normal));
                tv_name.setTextColor(ArmsUtils.getColor(mContext, R.color.theme_white_color));
            }
            tv_name.setTextSize(FontSize);
            tv_name.setText(desc);
            tv_name.setGravity(Gravity.CENTER);
        }

        private void initModifierSoldout(Modifier datasBean) {

            tv_soldout.setMinHeight(soldoutModFlagHeight);
            tv_soldout.setTextSize(OrderNumberChildFontSize);
            tv_soldout.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.meal_shape_remaining_number_bg));
            tv_soldout.setGravity(Gravity.CENTER);

            tv_soldout.setVisibility(View.INVISIBLE);
            iv_qty_sold.setVisibility(View.INVISIBLE);
            blv_soldout.setVisibility(View.INVISIBLE);

            String qty = datasBean.getCurrentMaxNumber();
            if (TextUtils.isEmpty(qty)) {
                tv_soldout.setVisibility(View.INVISIBLE);
            } else {
                if (qty.equalsIgnoreCase("售罄") || qty.equalsIgnoreCase("暫停")) {
                    blv_soldout.setLableText(qty);
                    blv_soldout.setVisibility(View.VISIBLE);
                } else {
                    tv_soldout.setVisibility(View.VISIBLE);
                    tv_soldout.setText(qty);
                }
            }
        }

        private void initModifierLayoutParams() {
            ViewGroup.LayoutParams layoutParams = tv_name.getLayoutParams();
            //这里减2 减去分割线宽度
//            layoutParams.width = ColWidth - 4;
            layoutParams.height = ColHeight;
            tv_name.setLayoutParams(layoutParams);
        }
    }

    private OnItemClickListener mOnItemClickListener;

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

    public interface OnItemClickListener {

        void onItemClick(Modifier datasBean, int position);

    }
}
