package com.ccwangluo.im.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ccwangluo.ccim.CCIMClient;
import com.ccwangluo.ccim.listener.ResultEventCallback;
import com.ccwangluo.ccim.modle.MessageContent;
import com.ccwangluo.im.R;

import java.util.List;

public class PrivateMsgAdapter extends BaseAdapter {

    private List<MessageContent> dataList;

    private Context context;

    public PrivateMsgAdapter(List<MessageContent> dataList, Context context) {
        this.dataList = dataList;
        this.context = context;
    }

    /**
     * Provide a reference to the type of views that you are using
     * (custom ViewHolder).
     */
    public static class ViewHolder {
        public TextView msg_content;
        public TextView msg_content_send;

    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

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

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder mHolder;
        if (convertView == null) {
            mHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.msg_row_item, null, true);
            mHolder.msg_content = convertView.findViewById(R.id.msg_content);
            mHolder.msg_content_send = convertView.findViewById(R.id.msg_content_send);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        MessageContent message = dataList.get(position);
        if (message.getSenderUserId() != null) {
            mHolder.msg_content.setVisibility(View.VISIBLE);
            mHolder.msg_content_send.setVisibility(View.GONE);
        } else if (message.getReciveUserId() != null) {
            mHolder.msg_content.setVisibility(View.GONE);
            mHolder.msg_content_send.setVisibility(View.VISIBLE);
        }
        if (message.getMsgType() == MessageContent.MessageType.MESSAGE_TYPE_TEXT) {
            mHolder.msg_content.setText(message.getContent());
        } else {
            mHolder.msg_content.setText("语音消息");
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (message.getMsgType() == MessageContent.MessageType.MESSAGE_TYPE_VOICE) {
                    CCIMClient.getInstance().startPlayAudio(message.getMsgId(), new ResultEventCallback<Void>() {
                        @Override
                        public void onSuccess(Void unused) {

                        }

                        @Override
                        public void onFailed(int errorCode) {

                        }
                    });
                }
            }
        });
        return convertView;
    }

    public void refreshData() {
        notifyDataSetChanged();
    }
}

