package com.easygbs.easygbd.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;

import com.easygbs.easygbd.R;
import com.easygbs.easygbd.dao.bean.Chan;
import com.easygbs.easygbd.viewadapter.CommonAdapter;
import com.easygbs.easygbd.viewadapter.ViewHolder;
import com.easygbs.easygbd.activity.MainActivity;

import java.util.List;

public class ChannelAdapter extends CommonAdapter<Chan> {
    private Context context;
    private MainActivity mMainActivity;
    public List<Chan> ChanList;

    private OnItemClickListener onItemClickListener;

    public ChannelAdapter(Context context, MainActivity mMainActivity, int layoutId, List<Chan> ChanList) {
        super(context, layoutId, ChanList);
        this.context = context;
        this.mMainActivity = mMainActivity;
        this.ChanList = ChanList;
    }

    @Override
    protected void convert(ViewHolder holder, Chan mChan, int position) {
        LinearLayout llroot = holder.getView(R.id.llroot);
        int selected = mChan.getSelected();

        if (selected == 0) {
            llroot.setBackgroundColor(Color.WHITE);
        } else {
            llroot.setBackgroundColor(mMainActivity.getResources().getColor(R.color.color_53c3a3));
        }

        holder.setText(R.id.tvid, "" + mChan.getEc())
                .setText(R.id.tvname, mChan.getNa())
                .setText(R.id.tvchannelid, mChan.getCid());

        // 设置点击事件
        llroot.findViewById(R.id.tvchannelid).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position, mChan, "channel_id"); // 传递名称被点击
                }
            }
        });

        llroot.findViewById(R.id.tvname).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, position, mChan, "channel_name"); // 传递 ID 被点击
                }
            }
        });

    }

    // 设置点击事件监听器
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void removeOnItemClickListener() {
        this.onItemClickListener = null;
    }


    // 定义点击事件接口
    public interface OnItemClickListener {
        void onItemClick(View v, int position, Chan chan, String type);
    }

}
