package com.ccwangluo.im.ui.dashboard;

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.Contact;
import com.ccwangluo.ccim.modle.MessageContent;
import com.ccwangluo.im.adapter.PrivateMsgAdapter;
import com.ccwangluo.im.data.LoginViewModel;
import com.ccwangluo.im.databinding.ActivityConnectBinding;
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 ConnectActivity extends AppCompatActivity {
    private ActivityConnectBinding binding;
    private LoginViewModel loginViewModel;
    private PrivateMsgAdapter privaetMsgAdapter;
    private List<MessageContent> list = new ArrayList<>();
    private String filePath;
    private boolean isSuccess = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityConnectBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        loginViewModel = new ViewModelProvider(this)
                .get(LoginViewModel.class);

        privaetMsgAdapter = new PrivateMsgAdapter(list,this);

        binding.msgList.setAdapter(privaetMsgAdapter);

        Contact contact = (Contact) getIntent().getSerializableExtra("contact");

        loginViewModel.privateMsgList.observe(this, new Observer<MessageContent>() {
            @Override
            public void onChanged(MessageContent messageContent) {
                if (messageContent.getSenderUserId().equals(contact.getContactUserId()) || messageContent.getReciveUserId().equals(contact.getContactUserId())) {
                    list.add(messageContent);
                    privaetMsgAdapter.refreshData();
                    binding.msgList.setSelection(privaetMsgAdapter.getCount() - 1);
                }
            }
        });

        binding.sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = binding.sendEdit.getText().toString();
                CCIMClient.getInstance().sendTextMessage(content, contact.getContactUserId(), contact.getContactUserName(), new ResultEventCallback<MessageContent>() {
                    @Override
                    public void onSuccess(MessageContent messageContent) {
                        binding.sendEdit.setText("");
                        loginViewModel.addPrivateMsg(messageContent);
                        ToastUtil.show(getApplication(), "send Success");
                    }

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

        binding.sendVoice.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (isSuccess)
                        CCIMClient.getInstance().stopAndSendAudioMessage(contact.getContactUserId(), contact.getContactUserName(), new ResultEventCallback<MessageContent>() {
                            @Override
                            public void onSuccess(MessageContent message) {
                                binding.sendEdit.setText("");
                                loginViewModel.addPrivateMsg(message);
                                ToastUtil.show(getApplication(), "send Success");
                            }

                            @Override
                            public void onFailed(int errorCode) {
                                ToastUtil.show(getApplication(), "send onFailed  :" + errorCode);
                            }
                        });
                }
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    CCIMClient.getInstance().startRecordAudioMessage(ConnectActivity.this, contact.getContactUserId(), contact.getContactUserName(), new ResultEventCallback<Void>() {
                        @Override
                        public void onSuccess(Void unused) {

                            isSuccess = true;
                        }

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

                                loginViewModel.addPrivateMsg(t);

                        }

                        @Override
                        public void onFailed(int errorCode) {

                        }
                    });
                }

                return false;
            }
        });
        CCIMClient.getInstance().getPrivateMessage(contact.getContactUserId(), new ResultEventCallback<List<MessageContent>>() {
            @Override
            public void onSuccess(List<MessageContent> messageContents) {
                for (MessageContent messageContent : messageContents) {
                    list.add(messageContent);
                }
                privaetMsgAdapter.refreshData();
                binding.msgList.setSelection(privaetMsgAdapter.getCount() - 1);
            }

            @Override
            public void onFailed(int errorCode) {

            }
        });
        EventBus.getDefault().register(this);

    }

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

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