package com.ccwangluo.im;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.ccwangluo.ccim.CCIMClient;
import com.ccwangluo.ccim.listener.OnConnectListener;
import com.ccwangluo.ccim.listener.OnPushMsgListener;
import com.ccwangluo.ccim.listener.ResultEventCallback;
import com.ccwangluo.ccim.modle.Contact;
import com.ccwangluo.ccim.modle.MessageContent;
import com.ccwangluo.ccim.modle.NetStatus;
import com.ccwangluo.ccim.modle.PullContactsRes;
import com.ccwangluo.ccim.modle.User;
import com.ccwangluo.ccim.util.HmacUtil;
import com.ccwangluo.im.databinding.FragmentFirstBinding;

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

public class FirstFragment extends Fragment {

    private FragmentFirstBinding binding;
    private static final String TAG = FirstFragment.class.getName();

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        binding = FragmentFirstBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        String appKey = "c981adfde9c4000";
        String userName = "user101";
        Long userId = 101L;
        String extData = "extData";
        Long ts = System.currentTimeMillis();
        String data = appKey + "&" + userId + "&" + userName + "&" + extData + "&" + ts +
                "&wW7cR31PwJseIoZMOs311nljHpwUmujs";
        //sign计算在服务器,本地不要暴露appsecrect
        String sign = HmacUtil.stringToSign("wW7cR31PwJseIoZMOs311nljHpwUmujs", data);

        CCIMClient.getInstance().initSDK(getContext(), appKey, userName, extData, userId, ts, sign, new OnConnectListener() {
            @Override
            public void onSocketConnection() {
                refreshLogView("connect success");
            }

            @Override
            public void onSocketDisconnection() {
                refreshLogView("disconnect");
            }
        });

        CCIMClient.getInstance().registerMessageListener(new OnPushMsgListener() {


            @Override
            public void pushPrivateMsg(MessageContent msg) {

            }

            @Override
            public void pushChannelMsg(MessageContent msg) {

            }

            @Override
            public void pushNewContact(Contact contact) {
                refreshLogView("new Contact " + contact);
            }

            @Override
            public void pushDeleteContact(Long contactId) {
                refreshLogView("delete contactId " + contactId);
            }

            @Override
            public void pushContactNetStatus(Long contactId, NetStatus status) {
                refreshLogView("ContactNetStatus is :" + contactId + " status:" + status);
            }

            @Override
            public void pushContactNewReq(User user) {

            }

            @Override
            public void pushBlockByContact(Long userId) {

            }

        });

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

            @Override
            public void onClick(View v) {
                CCIMClient.getInstance().getContactList(true, new ResultEventCallback<PullContactsRes>() {
                    @Override
                    public void onSuccess(PullContactsRes pullContactsRes) {

                    }

                    @Override
                    public void onFailed(int errorCode) {

                    }
                });
            }
        });

        binding.getOfflineMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                CCIMClient.getInstance().getOfflineMessage(0L, new ResultEventCallback<List<Message.PullOfflineMessageResponse.OfflineMessage>>() {
//                    @Override
//                    public void onSuccess(List<Message.PullOfflineMessageResponse.OfflineMessage> offlineMessages) {
//                        Log.d(TAG, "offlineMessages: " + offlineMessages.toString());
//                        refreshLogView("offlineMessages: " + offlineMessages.toString());
//                    }
//                });
            }
        });

        binding.searchUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                CCIMClient.getInstance().searchUser(binding.userName.getText().toString(), new ResultEventCallback<List<User>>() {
//                    @Override
//                    public void onSuccess(List<User> users) {
//                        refreshLogView(users.toString());
//                    }
//                });
            }
        });


        binding.addUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CCIMClient.getInstance().addContact(Long.parseLong(binding.userId.getText().toString()), new ResultEventCallback<Void>() {
                    @Override
                    public void onSuccess(Void unused) {

                    }

                    @Override
                    public void onFailed(int errorCode) {

                    }
                });
            }
        });

        binding.sendTextMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CCIMClient.getInstance().sendTextMessage(binding.msg.getText().toString(), Long.parseLong(binding.userId.getText().toString()), binding.userName.getText().toString(),
                        new ResultEventCallback<MessageContent>() {
                            @Override
                            public void onSuccess(MessageContent messageContent) {

                            }

                            @Override
                            public void onFailed(int errorCode) {

                            }
                        });
            }
        });

        binding.getContactRequests.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CCIMClient.getInstance().getContactReqList(new ResultEventCallback<List<User>>() {
                    @Override
                    public void onSuccess(List<User> contactRequestsResponse) {
                        refreshLogView("getContactRequests: " + contactRequestsResponse.toString());
                    }

                    @Override
                    public void onFailed(int errorCode) {

                    }
                });
            }
        });

        binding.checkContactRequests.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CCIMClient.getInstance().checkContactReq(Long.parseLong(binding.userId.getText().toString()), true, new ResultEventCallback<Void>() {
                    @Override
                    public void onSuccess(Void unused) {

                    }

                    @Override
                    public void onFailed(int errorCode) {

                    }
                });
            }
        });
        binding.createContactGroupRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CCIMClient.getInstance().createContactGroup("zhangsan", new ResultEventCallback<Long>() {
                    @Override
                    public void onSuccess(Long aLong) {
                        refreshLogView("createContactGroupRequest : " + aLong);
                    }

                    @Override
                    public void onFailed(int errorCode) {

                    }
                });
            }
        });

        binding.moveContactGroupRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CCIMClient.getInstance().moveContactGroup(Long.parseLong(binding.groupId.getText().toString()), Long.parseLong(binding.userId.getText().toString()),
                        new ResultEventCallback<Void>() {
                            @Override
                            public void onSuccess(Void unused) {

                            }

                            @Override
                            public void onFailed(int errorCode) {

                            }
                        });
            }
        });

        binding.deleteContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CCIMClient.getInstance().deleteContact(Long.parseLong(binding.userId.getText().toString()), new ResultEventCallback<Void>() {
                    @Override
                    public void onSuccess(Void unused) {

                    }

                    @Override
                    public void onFailed(int errorCode) {

                    }
                });
            }
        });

        binding.blockContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CCIMClient.getInstance().blockContact(Long.parseLong(binding.userId.getText().toString()), new ResultEventCallback<Void>() {
                    @Override
                    public void onSuccess(Void unused) {

                    }

                    @Override
                    public void onFailed(int errorCode) {

                    }
                });
            }
        });

        binding.reportIllegalUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CCIMClient.getInstance().reportIllegalUser("搞黄色", "搞黄色", Long.parseLong(binding.userId.getText().toString()),
                        new ResultEventCallback<Void>() {
                            @Override
                            public void onSuccess(Void unused) {

                            }

                            @Override
                            public void onFailed(int errorCode) {

                            }
                        });
            }
        });


        binding.sendChannelMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                CCIMClient.getInstance().sendChannelMessage(binding.msg.getText().toString(), Long.parseLong(binding.channelId.getText().toString()), new ResultCallback() {
//                    @Override
//                    public void onSuccess() {
//                        refreshLogView("sendChannelMessage success");
//                    }
//                });
            }
        });
//
//        binding.startRecordAudioMessage.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                CCIMClient.getInstance().startRecordAudioMessage(getActivity(), new ResultEventCallback<Void>() {
//                            @Override
//                            public void onSuccess(Void unused) {
//
//                            }
//
//                            @Override
//                            public void onFailed(int errorCode) {
//
//                            }
//                        }
//                );
//            })
                    binding.stopAndSendAudioMessage.setOnClickListener(new View.OnClickListener()

            {
                @Override
                public void onClick (View v){

            }
            });

                binding.cancleAudioMessage.setOnClickListener(new View.OnClickListener()

            {
                @Override
                public void onClick (View v){
                CCIMClient.getInstance().cancleAudioMessage();
            }
            });

                binding.createChannel.setOnClickListener(new View.OnClickListener()

            {
                @Override
                public void onClick (View v){
                ArrayList<Long> userIds = new ArrayList<>();
                userIds.add(Long.parseLong(binding.userId.getText().toString()));
                CCIMClient.getInstance().createChannel("大世界", "112", false, userIds, new ResultEventCallback<Long>() {
                    @Override
                    public void onSuccess(Long aLong) {
                        binding.channelId.setText(aLong.toString());
                        refreshLogView("createChannel " + aLong);
                    }

                    @Override
                    public void onFailed(int errorCode) {

                    }
                });
            }
            });

                binding.joinChannel.setOnClickListener(new View.OnClickListener()

            {
                @Override
                public void onClick (View v){
                CCIMClient.getInstance().joinChannel(Long.parseLong(binding.channelId.getText().toString()), new ResultEventCallback<Void>() {
                    @Override
                    public void onSuccess(Void unused) {

                    }

                    @Override
                    public void onFailed(int errorCode) {

                    }
                });
            }
            });

                binding.removeChannelMember.setOnClickListener(new View.OnClickListener()

            {
                @Override
                public void onClick (View v){
                CCIMClient.getInstance().removeChannelMember(Long.parseLong(binding.channelId.getText().toString()), Long.parseLong(binding.userId.getText().toString()),
                        new ResultEventCallback<Void>() {
                            @Override
                            public void onSuccess(Void unused) {

                            }

                            @Override
                            public void onFailed(int errorCode) {

                            }
                        });
            }
            });

                binding.exitChannel.setOnClickListener(new View.OnClickListener()

            {
                @Override
                public void onClick (View v){
                CCIMClient.getInstance().exitChannel(Long.parseLong(binding.channelId.getText().toString()), new ResultEventCallback<Void>() {
                    @Override
                    public void onSuccess(Void unused) {

                    }

                    @Override
                    public void onFailed(int errorCode) {

                    }
                });
            }
            });
        }

        public void refreshLogView (String msg){
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    binding.txLog.append(msg + "\r\n");
                    int offset = binding.txLog.getLineCount() * binding.txLog.getLineHeight();
                    if (offset > binding.txLog.getHeight()) {
                        binding.txLog.scrollTo(0, offset - binding.txLog.getHeight() + binding.txLog.getLineHeight() * 2);
                    }
                }
            });


        }

        @Override
        public void onDestroyView () {
            super.onDestroyView();
            binding = null;
        }

    }
