package com.bokecc.livemodule.live.morefunction;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.live.DWLiveCoreHandler;
import com.bokecc.livemodule.live.DWLiveMoreFunctionListener;
import com.bokecc.livemodule.live.chat.KeyboardHeightObserver;
import com.bokecc.livemodule.live.chat.module.ChatEntity;
import com.bokecc.livemodule.live.morefunction.fab.FabAttributes;
import com.bokecc.livemodule.live.morefunction.fab.MoreFunctionFab;
import com.bokecc.livemodule.live.morefunction.fab.OnFabClickListener;
import com.bokecc.livemodule.live.morefunction.privatechat.LivePrivateChatLayout;
import com.bokecc.livemodule.live.morefunction.rtc.RTCControlLayout;
import com.bokecc.livemodule.live.room.NoticeFloatLayout;
import com.bokecc.livemodule.utils.DensityUtil;
import com.bokecc.livemodule.view.BaseRelativeLayout;
import com.bokecc.sdk.mobile.live.DWLive;
import com.bokecc.sdk.mobile.live.pojo.PrivateChatInfo;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

/**
 * 更多功能 布局 （公告、私聊）
 */
public class MoreFunctionLayout extends BaseRelativeLayout implements OnFabClickListener, DWLiveMoreFunctionListener, KeyboardHeightObserver {

    private static final int RTC = 2;



    private RTCControlLayout mRTCControlLayout;

    private MoreFunctionFab mFabTop;
    private LinearLayout mRemindRoot;


    /**
     * 消息提醒的点击事件
     */
    private RemindItem.RemindListener remindListener = new RemindItem.RemindListener() {
        @Override
        public void onClickListener(RemindItem remindItem, RemindItem.RemindType type) {
            mRemindRoot.removeView(remindItem);
            if (mRemindRoot.getChildCount() <= 0) {
                mRemindRoot.setVisibility(GONE);
            }
        }

        @Override
        public void onClose(RemindItem remindItem) {
            mRemindRoot.removeView(remindItem);
            if (mRemindRoot.getChildCount() <= 0) {
                mRemindRoot.setVisibility(GONE);
            }
        }
    };

    public MoreFunctionLayout(Context context) {
        super(context);
        initMoreFunctionListener();
    }

    public MoreFunctionLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initMoreFunctionListener();
    }

    public MoreFunctionLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initMoreFunctionListener();
    }

    @Override
    public void initViews() {
        LayoutInflater.from(mContext).inflate(R.layout.live_portrait_more_function, this, true);
        mRemindRoot = findViewById(R.id.ll_remind);
        mRTCControlLayout = findViewById(R.id.rtc_layout);

        mFabTop = findViewById(R.id.fab_top);



        FabAttributes rtc = new FabAttributes.Builder()
                .setBackgroundTint(Color.parseColor("#FFFFFF"))
                .setSrc(getResources().getDrawable(R.drawable.more_function_rtc))
                .setFabSize(FloatingActionButton.SIZE_MINI)
                .setPressedTranslationZ(0)
                .setTag(RTC)
                .build();

        mFabTop.addFab(rtc);

        mFabTop.setFabClickListener(this);

    }

    /**
     * 根据类型 展示消息提醒
     */
    private void showRemindByType(final RemindItem.RemindType remindType) {
        post(new Runnable() {
            @Override
            public void run() {
                if (mRemindRoot.getVisibility() != VISIBLE) {
                    mRemindRoot.setVisibility(VISIBLE);
                }
                if (mRemindRoot.getChildCount() > 0) {
                    for (int i = 0; i < mRemindRoot.getChildCount(); i++) {
                        if (mRemindRoot.getChildAt(i) instanceof RemindItem) {
                            RemindItem remindItem = (RemindItem) mRemindRoot.getChildAt(i);
                            if (remindItem.getType() == remindType) {
                                return;
                            }
                        }
                    }
                }
                RemindItem remindItem = new RemindItem(getContext());
                remindItem.setContent(remindType);
                remindItem.setRemindListener(remindListener);
                mRemindRoot.addView(remindItem, 0);
            }
        });
    }

    /**
     * 根据类型 移除消息提醒
     */
    private void removeRemindByType(final RemindItem.RemindType remindType) {
        post(new Runnable() {
            @Override
            public void run() {
                RemindItem remindItem = null;
                if (mRemindRoot.getChildCount() > 0) {
                    for (int i = 0; i < mRemindRoot.getChildCount(); i++) {
                        if (mRemindRoot.getChildAt(i) instanceof RemindItem) {
                            RemindItem remindItemTemp = (RemindItem) mRemindRoot.getChildAt(i);
                            if (remindItemTemp.getType() == remindType) {
                                remindItem = remindItemTemp;
                                break;
                            }
                        }
                    }
                }
                if (remindItem != null) {
                    mRemindRoot.removeView(remindItem);
                }
            }
        });
    }



    @Override
    public void onFabClick(FloatingActionButton fab, Object tag) {
        if (tag.equals(RTC)) {
            if (mRTCControlLayout.getVisibility() == VISIBLE) {
                mRTCControlLayout.setVisibility(GONE);
            } else {
                DWLiveCoreHandler dwLiveCoreHandler = DWLiveCoreHandler.getInstance();
                if (dwLiveCoreHandler != null && dwLiveCoreHandler.isAllowRtc() && !dwLiveCoreHandler.isMultiRtc()) {
                    hideAll();
                    mRTCControlLayout.setVisibility(VISIBLE);
                } else {
                    toastOnUiThread("主播未开通连麦");
                }
            }
        }
    }

    @Override
    public void onFabVisible(boolean visible) {
        if (!visible) {
            mRTCControlLayout.setVisibility(GONE);
        }
        if (visible) {
            mRemindRoot.setTranslationY(0);
        } else {
            mRemindRoot.setTranslationY(mFabTop.getHeight() - mFabTop.getChildAt(0).getHeight() - DensityUtil.dp2px(getContext(), 10));
        }

    }

    /**
     * 初始化更多功能监听
     */
    private void initMoreFunctionListener() {
        DWLiveCoreHandler dwLiveCoreHandler = DWLiveCoreHandler.getInstance();
        if (dwLiveCoreHandler != null) {
            dwLiveCoreHandler.setDwLiveMoreFunctionListener(this);
        }
    }


    //***************************** 实现 DWLiveMoreFunctionListener 定义的方法 *************************************/

    @Override
    public void onKeyboardHeightChanged(int height, int orientation) {
        if (height>10){
            //键盘弹起，收起Fab按钮
            if (mFabTop!=null){
                mFabTop.closeAnimate();
            }
        }
    }

    public RTCControlLayout getRTCControlLayout() {
        return mRTCControlLayout;
    }



    private void hideAll(){
        mRTCControlLayout.setVisibility(GONE);
    }


    @Override
    public void onAnnouncement(boolean isRemove, String announcement) {

    }
}
