package com.ccwangluo.im.ui.dashboard;

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

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.ccwangluo.ccim.CCIMClient;
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.FragmentConnectBinding;
import com.ccwangluo.im.util.ToastUtil;

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

public class ConnectFragment extends Fragment {
    private FragmentConnectBinding binding;
    private LoginViewModel loginViewModel;
    private PrivateMsgAdapter privaetMsgAdapter;
    private List<MessageContent> list = new ArrayList<>();
    private String filePath;

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentConnectBinding.inflate(inflater, container, false);
        loginViewModel = new ViewModelProvider(getActivity())
                .get(LoginViewModel.class);

        privaetMsgAdapter = new PrivateMsgAdapter(list, getContext());

        binding.msgList.setAdapter(privaetMsgAdapter);

        Contact contact = (Contact) getArguments().getSerializable("contact");

        loginViewModel.privateMsgList.observe(getViewLifecycleOwner(), 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(getContext(), "send Success");
                    }

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

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

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

                        }

                        @Override
                        public void onFailed(int errorCode) {

                        }
                    });
                }

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

            @Override
            public void onFailed(int errorCode) {

            }
        });

        return binding.getRoot();
    }
}