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.startVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                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(ConnectActivity.this, errorCode + "");
                    }
                }, new RecordAutoSendCallback() {
                    @Override
                    public void onSuccess(MessageContent t) {

                        loginViewModel.addPrivateMsg(t);

                    }

                    @Override
                    public void onFailed(int errorCode) {

                    }
                });
            }
        });

        binding.sendVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                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);
                        }
                    });
            }
        });

        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);
                MessageContent messageContent = messageContents.get(messageContents.size() - 1);
                CCIMClient.getInstance().updateReadOffset(messageContent.getSenderUserId(), messageContent.getMsgId(), new ResultEventCallback<Void>() {
                    @Override
                    public void onSuccess(Void unused) {

                    }

                    @Override
                    public void onFailed(int errorCode) {

                    }
                });
            }

            @Override
            public void onFailed(int errorCode) {

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

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(MessageContent message) {
        loginViewModel.addPrivateMsg(message);
        CCIMClient.getInstance().updateReadOffset(message.getSenderUserId(), message.getMsgId(), new ResultEventCallback<Void>() {
            @Override
            public void onSuccess(Void unused) {
                System.out.println("updateReadOffset success");
            }

            @Override
            public void onFailed(int errorCode) {
                System.out.println("updateReadOffset onFailed");
            }
        });
    }

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