package com.ewei.demo.ticket.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ewei.demo.R;
import com.ewei.sdk.base.BaseListAdapter;
import com.ewei.sdk.base.EweiDeskInfo;
import com.ewei.sdk.entity.NameValue;
import com.ewei.sdk.utility.Utils;


/**
 * Created by Administrator on 2016/8/30.
 */
public class OpenTicketPriorityListAdapter extends BaseListAdapter<NameValue> {

    private NameValue mPriority;

    public OpenTicketPriorityListAdapter(Context context, NameValue priority) {
        super(context);
        mPriority = priority;
    }

    /**
     * 点击优先级时的处理
     *
     * @param
     * @return
     */
    public void clickItem(int position) {

        NameValue priority = (NameValue) getItem(position);
        if (mPriority != null && !TextUtils.isEmpty(mPriority.name)) {
            if (Utils.equals(mPriority.name, priority.name)) {
                mPriority = null;
            } else {
                mPriority = priority;
            }
        } else {
            mPriority = priority;
        }
        notifyDataSetChanged();
    }

    public void clickItemRequired(int position) {
        mPriority = (NameValue) getItem(position);
        notifyDataSetChanged();
    }


    public NameValue getclickItem() {
        return mPriority;
    }

    @Override
    protected int getLayout() {
        return R.layout.ewei_open_listview_item_ticket_priority;
    }

    @Override
    protected AbstractViewHolder getViewHolder(View convertView) {
        ItemViewHolder ivh = new ItemViewHolder();
        ivh.mIvOption = (ImageView) convertView.findViewById(R.id.iv_ticket_priority_option);
        ivh.mTvName = (TextView) convertView.findViewById(R.id.tv_ticket_priority_name);
        return ivh;
    }

    @Override
    protected void bindView(AbstractViewHolder viewHolder, NameValue nameValue, int position) {
        ItemViewHolder ivh = (ItemViewHolder) viewHolder;
        ivh.mIvOption.setBackgroundColor(Color.TRANSPARENT);
        ivh.mIvOption.setImageResource(com.ewei.sdk.R.drawable.shape_round_text_bk_uncheck);
        if (mPriority != null && Utils.equals(nameValue.name, mPriority.name)) {
            ivh.mIvOption.setImageResource(com.ewei.sdk.R.mipmap.ewei_ic_ticketstatus_check);
            ivh.mIvOption.setBackgroundResource(com.ewei.sdk.R.drawable.shape_round_text_bk);
            GradientDrawable drawable = (GradientDrawable) ivh.mIvOption.getBackground();
            drawable.setColor(EweiDeskInfo.getmEweiKeyColor());
        }
        ivh.mTvName.setText(nameValue.name);
    }

    private class ItemViewHolder extends AbstractViewHolder {
        ImageView mIvOption;
        TextView mTvName;
    }
}
