package com.tencent.qcloud.tuikit.tuichat.ui.page;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.tencent.qcloud.tuicore.TUIConstants;
import com.tencent.qcloud.tuicore.util.ScreenUtil;
import com.tencent.qcloud.tuikit.tuichat.TUIChatConstants;
import com.tencent.qcloud.tuikit.tuichat.bean.ChatInfo;
import com.tencent.qcloud.tuikit.tuichat.presenter.C2CChatPresenter;
import com.tencent.qcloud.tuikit.tuichat.ui.view.ChatView;
import com.tencent.qcloud.tuikit.tuichat.util.ChatMessageBuilder;
import com.tencent.qcloud.tuikit.tuichat.util.TUIChatLog;
import com.zxhl.cms.localmodle.EventBusUpdateXinDongZhiBean;
import com.zxhl.cms.net.ApiClient;
import com.zxhl.cms.net.RxSchedulers;
import com.zxhl.cms.net.SettingPreference;
import com.zxhl.cms.net.callback.BaseObserver;
import com.zxhl.cms.net.model.chat.ChatMessageStarEntity;
import com.zxhl.cms.utils.JumpUtils;
import com.zxhl.cms.utils.Utils;
import com.zxhl.cms.widget.ChatDialogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class TUIC2CChatFragment extends TUIBaseChatFragment {

    private static final String TAG = TUIC2CChatFragment.class.getSimpleName();

    private ChatInfo chatInfo;
    private C2CChatPresenter presenter;
    private String conversationHeadIconUrl;
    private String messageContent;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        TUIChatLog.i(TAG, "oncreate view " + this);

        baseView = super.onCreateView(inflater, container, savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle == null) {
            return baseView;
        }
        chatInfo = (ChatInfo) bundle.getSerializable(TUIChatConstants.CHAT_INFO);
        conversationHeadIconUrl = bundle.getString("conversationHeadIconUrl");
        messageContent = bundle.getString(TUIConstants.TUIChat.MESSAGE_CONTENT);
        if (chatInfo == null) {
            return baseView;
        }

        initView();

        return baseView;
    }

    @Override
    protected void initView() {
        super.initView();
        EventBus.getDefault().register(this);
        titleBar.getLeftGroup().setMinimumWidth(ScreenUtil.dip2px(42f));
        titleBar.getRightGroup().setMinimumWidth(ScreenUtil.dip2px(42f));
        titleBar.getRightGroup().setVisibility(View.INVISIBLE);
        titleBar.getMiddleLeftGroup().setVisibility(View.VISIBLE);
        titleBar.getMiddleRightGroup().setVisibility(View.VISIBLE);
        int spaceWidth = ScreenUtil.getScreenWidth(getContext()) - ScreenUtil.dip2px(42) * 2;
        titleBar.getMiddleLeftGroup().post(() -> {
            int leftWidth = titleBar.getMiddleLeftGroup().getWidth();
            int rightWidth = titleBar.getMiddleRightGroup().getWidth();
            if (spaceWidth - leftWidth - rightWidth <= 0) {
                titleBar.getMiddleTitle().setMinWidth(ScreenUtil.dip2px(72));
            }
        });
//        titleBar.setOnRightClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Bundle bundle = new Bundle();
//                bundle.putString("chatId", chatInfo.getId());
//                TUICore.startActivity("FriendProfileActivity", bundle);
//            }
//        });
        titleBar.getMiddleParentGroup().setOnClickListener(v -> {
            if (chatInfo == null) {
                return;
            }
            JumpUtils.UserDetailJump(chatInfo.getId(), "0");
        });

        chatView.setPresenter(presenter);
        presenter.setChatInfo(chatInfo);
        chatView.setChatInfo(chatInfo);
        titleBar.getLocationInWindow(ChatView.titleBarLayoutLoc);
        chatView.getMessageLayout().setRightChatContentFontColor(Color.parseColor("#FFFFFF"));
        chatView.getMessageLayout().setLeftChatContentFontColor(Color.parseColor("#2E2F33"));
        chatView.getMessageLayout().setChatContextFontSize(14);
        chatView.getMessageLayout().setChatTimeFontColor(Color.parseColor("#666666"));
        chatView.getMessageLayout().setChatTimeFontSize(13);
        //系统消息
        if (getChatInfo().getId().equalsIgnoreCase("administrator")) {
            chatView.getInputLayout().setVisibility(View.GONE);
            titleBar.getMiddleLeftGroup().setVisibility(View.INVISIBLE);
            titleBar.getMiddleRightGroup().setVisibility(View.INVISIBLE);
            titleBar.getMiddleTitle().setText("系统消息");
        } else {
            chatView.getInputLayout().setVisibility(View.VISIBLE);
            titleBar.getMiddleLeftGroup().setVisibility(View.VISIBLE);
            titleBar.getMiddleRightGroup().setVisibility(View.VISIBLE);
            updateTitleBarView();
        }
//        chatView.getNoticeLayout().alwaysShow(true);
//        chatView.getNoticeLayout().getContentExtra().setVisibility(View.GONE);
//        chatView.getNoticeLayout().getContent().setText("文明交友，谨防诈骗。发现用户提供色情服务、传播淫秽信息或要求金钱往来时，请及时举报。");
        if (!TextUtils.isEmpty(messageContent)) {
            chatView.sendMessage(ChatMessageBuilder.buildTextMessage(messageContent), false);
        }
    }

    /**
     * 输入更多是否显示
     *
     * @return
     */
    public boolean isShowInputView() {
        if (chatView != null && chatView.getInputLayout() != null && chatView.getInputLayout().isShowInputMoreView()) {
            return true;
        }
        return false;
    }

    /**
     * 隐藏更多输入选项
     */
    public void hideInputMoreView() {
        if (isShowInputView()) {
            chatView.getInputLayout().hideSoftInput();
            chatView.getInputLayout().hideInputMoreLayout();
        }
    }


    public void setPresenter(C2CChatPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public C2CChatPresenter getPresenter() {
        return presenter;
    }

    @Override
    public ChatInfo getChatInfo() {
        return chatInfo;
    }

    /**
     * 请求心动值数据
     */
    private void updateTitleBarView() {
        if (chatInfo == null) {
            return;
        }
        ApiClient.INSTANCE.getChatApi().getChatMessageStarInfo(chatInfo.getId()).compose(RxSchedulers.observableIO2Main()).subscribe(new BaseObserver<ChatMessageStarEntity>() {
            @Override
            public void onSuccess(ChatMessageStarEntity result) {
                if (result != null) {
                    titleBar.getMiddleLeftTitle().setText(result.intimacy + "℃");
                    titleBar.getMiddleRightTitle().setText(result.beckoning + "℃");
                    //亲密度弹窗
                    if (result.popupIntimacy == 1) {
                        ChatDialogUtils.showMessageStarTipDialog(getActivity(), conversationHeadIconUrl, SettingPreference.getUserHeadImg(), "亲密值", result.intimacy + "℃");
                    }
                    //心动值弹窗
                    else if (result.popupBeckoning == 1) {
                        ChatDialogUtils.showMessageStarTipDialog(getActivity(), conversationHeadIconUrl, SettingPreference.getUserHeadImg(), "心动值", result.beckoning + "℃");
                    }
                }
            }

            @Override
            public void onFailure(Throwable e, String code, String errorMsg) {
                Utils.showToast(getContext(), errorMsg);
            }
        });
    }

    /**
     * 更新心动值
     *
     * @param eventBean
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateTitleBarData(EventBusUpdateXinDongZhiBean eventBean) {
        updateTitleBarView();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
