package com.ccwangluo.im.ui.notifications;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.ccwangluo.ccim.CCIMClient;
import com.ccwangluo.ccim.listener.RecordAutoSendCallback;
import com.ccwangluo.ccim.listener.ResultEventCallback;
import com.ccwangluo.ccim.modle.MessageContent;
import com.ccwangluo.im.adapter.ChannelMsgAdapter;
import com.ccwangluo.im.data.LoginViewModel;
import com.ccwangluo.im.databinding.ActivityConnectChannelBinding;
import com.ccwangluo.im.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class ConnectChannelActivity extends AppCompatActivity {
    private ActivityConnectChannelBinding binding;
    private LoginViewModel loginViewModel;
    private ChannelMsgAdapter channelMsgAdapter;
    private List<MessageContent> list = new ArrayList<>();
    private boolean isSuccess;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityConnectChannelBinding.inflate(getLayoutInflater());
        loginViewModel = new ViewModelProvider(this)
                .get(LoginViewModel.class);
        setContentView(binding.getRoot());

        channelMsgAdapter = new ChannelMsgAdapter(list, this);

        binding.msgList.setAdapter(channelMsgAdapter);

        long channelId = getIntent().getLongExtra("channelId", 0);

        loginViewModel.channelMsgList.observe(this, new Observer<MessageContent>() {
            @Override
            public void onChanged(MessageContent messageContent) {
                if (messageContent.getChannelId().equals(channelId)) {
                    list.add(messageContent);
                    channelMsgAdapter.refreshData();
                    binding.msgList.setSelection(channelMsgAdapter.getCount() - 1);
                }
            }
        });

        binding.sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = binding.sendEdit.getText().toString();
                CCIMClient.getInstance().sendChannelMessage(content, channelId, new ResultEventCallback<MessageContent>() {
                    @Override
                    public void onSuccess(MessageContent messageContent) {
                        binding.sendEdit.setText("");
                        loginViewModel.addChannelMsg(messageContent);
                        ToastUtil.show(ConnectChannelActivity.this, "send Success");
                    }

                    @Override
                    public void onFailed(int errorCode) {
                        ToastUtil.show(ConnectChannelActivity.this, "send onFailed  :" + errorCode);
                    }
                });
            }
        });

        CCIMClient.getInstance().getChannelMessage(channelId, new ResultEventCallback<List<MessageContent>>() {
            @Override
            public void onSuccess(List<MessageContent> messageContents) {
                list.addAll(messageContents);
                channelMsgAdapter.refreshData();
                binding.msgList.setSelection(channelMsgAdapter.getCount() - 1);
            }

            @Override
            public void onFailed(int errorCode) {

            }
        });

        binding.startVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CCIMClient.getInstance().startRecordChannelAudioMessage(ConnectChannelActivity.this, channelId,
                        new ResultEventCallback<Void>() {
                            @Override
                            public void onSuccess(Void unused) {
                                isSuccess = true;
                            }

                            @Override
                            public void onFailed(int errorCode) {
                                isSuccess = false;
                                ToastUtil.show(ConnectChannelActivity.this, errorCode + "");
                            }
                        }, new RecordAutoSendCallback() {
                            @Override
                            public void onSuccess(MessageContent t) {
                                loginViewModel.addChannelMsg(t);
                            }

                            @Override
                            public void onFailed(int errorCode) {

                            }
                        });
            }
        });

        binding.sendVoice.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isSuccess)
                    CCIMClient.getInstance().stopAndSendChannelAudioMessage(channelId, new ResultEventCallback<MessageContent>() {
                        @Override
                        public void onSuccess(MessageContent message) {
                            binding.sendEdit.setText("");
                            loginViewModel.addChannelMsg(message);
                            ToastUtil.show(ConnectChannelActivity.this, "send Success");
                        }

                        @Override
                        public void onFailed(int errorCode) {
                            ToastUtil.show(ConnectChannelActivity.this, "send onFailed  :" + errorCode);
                        }
                    });
            }
        });
        EventBus.getDefault().register(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(MessageContent message) {
        loginViewModel.addChannelMsg(message);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}