package com.ccwangluo.im.adapter;


import android.content.Context;
import android.graphics.Color;
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.OnPlayAudioCallback;
import com.ccwangluo.ccim.modle.MessageContent;
import com.ccwangluo.im.R;

import java.util.List;

public class ChannelMsgAdapter extends BaseAdapter {

    private List<MessageContent> dataList;

    private Context context;


    public ChannelMsgAdapter(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 messageContent = dataList.get(position);
        mHolder.msg_content.setVisibility(View.VISIBLE);
//        mHolder.msg_content_send.setVisibility(View.GONE);
        if (messageContent.getMsgType() == MessageContent.MessageType.MESSAGE_TYPE_TEXT) {
            mHolder.msg_content.setText(messageContent.getContent()+"\next_data:"+messageContent.getExtData());
        } else {
//            mHolder..setText(messageContent.getVoiceContent());
        }

        if (messageContent != null){
            mHolder.msg_content_send.setText(messageContent.getCreatedDate().toString());
        } else {
            mHolder.msg_content_send.setText("");
        }

        if (messageContent.getSenderUser() != null && messageContent.getSenderUser().getUserType() == 1) {
            convertView.setBackgroundColor(Color.CYAN);
        } else {
            convertView.setBackgroundColor(Color.WHITE);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (messageContent.getMsgType() == MessageContent.MessageType.MESSAGE_TYPE_VOICE) {
                    CCIMClient.getInstance().startPlayAudio(messageContent.getMsgId(), new OnPlayAudioCallback() {
                        @Override
                        public void onStart() {
                            System.out.println("start");
                        }

                        @Override
                        public void onSuccess() {
                            System.out.println("success");
                        }

                        @Override
                        public void onFailed(int errorCode) {
                            System.out.println("onFailed");
                        }
                    }, 1.0f);
                }
            }
        });
        return convertView;
    }

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

