package com.ccwangluo.im.ui.notifications;

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.MessageContent;
import com.ccwangluo.im.adapter.ChannelMsgAdapter;
import com.ccwangluo.im.data.LoginViewModel;
import com.ccwangluo.im.databinding.FragmentChannelConnectBinding;
import com.ccwangluo.im.util.ToastUtil;

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

public class ConnectChannelFragment extends Fragment {
    private FragmentChannelConnectBinding binding;
    private LoginViewModel loginViewModel;
    private ChannelMsgAdapter channelMsgAdapter;
    private List<MessageContent> list = new ArrayList<>();

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

    }

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

        channelMsgAdapter = new ChannelMsgAdapter(list, getContext());

        binding.msgList.setAdapter(channelMsgAdapter);

        long channelId = getArguments().getLong("channelId");

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

                    @Override
                    public void onFailed(int errorCode) {
                        ToastUtil.show(getContext(), "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.sendVoice.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    CCIMClient.getInstance().stopAndSendChannelAudioMessage(channelId, new ResultEventCallback<MessageContent>() {
                        @Override
                        public void onSuccess(MessageContent message) {
                            binding.sendEdit.setText("");
                            loginViewModel.addChannelMsg(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;
            }
        });

        return binding.getRoot();
    }
}