package com.ihaoin.hooloo.device.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.bumptech.glide.Glide;
import com.ihaoin.hooloo.device.R;
import com.ihaoin.hooloo.device.data.TrolleyGoods;
import com.ihaoin.hooloo.device.util.CollectionUtils;
import com.ihaoin.hooloo.device.util.StringAppend;
import com.ihaoin.hooloo.device.util.Utils;
import com.ihaoin.hooloo.device.view.TrolleyView;

import java.util.Arrays;
import java.util.List;

public class TrolleyAdapter extends BaseAdapter {
    private Context mContext;
    private TrolleyView trolleyView;

    public TrolleyAdapter(Context context, TrolleyView trolleyView) {
        this.mContext = context;
        this.trolleyView = trolleyView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TrolleyGoods goods = (TrolleyGoods) this.getItem(position);
        convertView = LayoutInflater.from(mContext).inflate(R.layout.item_trolley, null);
        ToggleButton butnState = convertView.findViewById(R.id.butn_state);
        ImageView imgThumbnail = convertView.findViewById(R.id.img_thumbnail);
        TextView txtName = convertView.findViewById(R.id.txt_name);
        TextView txtOptions = convertView.findViewById(R.id.txt_options);
        TextView icoPrice = convertView.findViewById(R.id.ico_price);
        TextView txtPrice = convertView.findViewById(R.id.txt_price);
        TextView txtDiscount = convertView.findViewById(R.id.txt_discount);
        View butnSubtract = convertView.findViewById(R.id.butn_subtract);
        TextView txtCount = convertView.findViewById(R.id.txt_count);
        View butnAdd = convertView.findViewById(R.id.butn_add);

        butnState.setChecked(goods.getChecked());
        butnState.setOnCheckedChangeListener((buttonView, isChecked) -> {
            goods.setChecked(isChecked);
            if (isChecked) {
                if (!trolleyView.checkGoodsCount()) { // 购物车选中数量超过阈值，不允许选中
                    goods.setChecked(false);
                    butnState.setChecked(false);
                    return;
                }
            }
            trolleyView.goodsChanged();
        });
        txtName.setText(goods.getName());
        if (!CollectionUtils.isEmpty(goods.getRules())) {
            StringAppend stringAppend = new StringAppend("/");
            goods.getRules().forEach(x -> stringAppend.append(x.getRuleName()));
            txtOptions.setText(stringAppend.toString());
            txtOptions.setVisibility(View.VISIBLE);
        } else {
            txtOptions.setVisibility(View.GONE);
        }

        txtPrice.setText(Utils.toString(goods.getPrice()));
        txtDiscount.setText(Utils.toString(goods.getDiscount()));

        icoPrice.setPaintFlags(icoPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        txtPrice.setPaintFlags(txtPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        txtCount.setText(goods.getCount().toString());
        butnSubtract.setOnClickListener(v -> trolleyView.subtractGoods(position));
        butnAdd.setOnClickListener(v -> trolleyView.addGoods(position));
        Glide.with(mContext).load(goods.getPics().getThumbnail()).into(imgThumbnail);

        ViewGroup layoutItem = convertView.findViewById(R.id.layout_item);
        Utils.setTouchDelegate(layoutItem, Arrays.asList(butnAdd, butnSubtract));

        return convertView;
    }

    private List<TrolleyGoods> getTrolleyGoods() {
        return trolleyView.getTrolleyGoods();
    }

    @Override
    public boolean isEmpty() {
        return getTrolleyGoods() == null || getTrolleyGoods().isEmpty() ? true : false;
    }

    @Override
    public int getCount() {
        return isEmpty() ? 0 : getTrolleyGoods().size();
    }

    @Override
    public Object getItem(int position) {
        return isEmpty() ? null : getTrolleyGoods().get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}
