package com.daya.live_teaching.ui.fragmnet;


import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cooleshow.base.ui.fragment.BaseFragment;
import com.daya.live_teaching.databinding.FragmentBigVideoWindowBinding;
import com.daya.live_teaching.model.ClassMember;
import com.daya.live_teaching.model.ScreenDisplay;
import com.daya.live_teaching.ui.view.ClassVideoListItem;
import com.daya.live_teaching.ui.view.RadioRtcVideoView;
import com.daya.live_teaching.utils.log.SLog;
import com.daya.live_teaching.viewmodel.ClassViewModel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

/**
 * 大窗口界面
 */
public class BigVideoWindowFragment extends BaseFragment<FragmentBigVideoWindowBinding> {

    private ClassVideoListItem videoViewItem;
    private ClassMember member;
    private OnWindowCloseListener listener;

    private static final String TAG = "big_video";
    private ConstraintLayout rootView;
    private ClassViewModel classViewModel;


    @Override
    protected FragmentBigVideoWindowBinding getLayoutView() {
        return FragmentBigVideoWindowBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView(View rootView) {
        rootView = mViewBinding.classBigWindowRoot;
        videoViewItem = mViewBinding.classVideoViewItem;
        videoViewItem.setVideViewToCenter();
        ImageView button = mViewBinding.classBtnClose;
        rootView.setOnClickListener(view -> {
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeBigWindow();
            }
        });
    }

    public void closeBigWindow() {
        SLog.d(TAG, "close big window");
        if (member != null) {
            RadioRtcVideoView videoView = VideoViewManager.getInstance().get(member.getUserId());
            if (videoView != null) {
                videoView.setZOrderOnTop(false);
                videoView.setZOrderMediaOverlay(false);
            }
        }
        videoViewItem.clear();
        if (listener != null) {
            listener.onClosed(member);
        }
        member = null;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }


    @Override
    public void initData() {
        classViewModel = ViewModelProviders.of(getActivity()).get(ClassViewModel.class);
        // 用户被踢
        classViewModel.getKickedOff().observe(this, new Observer<ClassMember>() {
            @Override
            public void onChanged(ClassMember classMember) {
                // 当用户被踢的时候， 则清理最后一帧
                if (member != null && member.getUserId().equals(classMember.getUserId())) {
                    RadioRtcVideoView videoView = VideoViewManager.getInstance().get(member.getUserId());
                    if (videoView != null) {
                        videoView.clearScreen();
                    } else {
                        videoViewItem.clear();
                    }
                }
            }
        });
    }

    private LinearLayout.LayoutParams getItemLayoutParams() {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return params;
    }

    /**
     * 设置窗口监听
     *
     * @param listener
     */
    public void setOnWindowCloseListener(OnWindowCloseListener listener) {
        this.listener = listener;
    }

    /**
     * 显示窗口操作
     */
    public void show(ClassMember member) {

        RadioRtcVideoView videoView = VideoViewManager.getInstance().get(member.getUserId());

        // 当有新的用户显示，要移除旧用户的， 并要通知视频列表刷新
        if (videoViewItem.getChildCount() > 0 && !videoViewItem.hasVideoView(videoView)) {
            videoViewItem.removeVideoView();
            if (listener != null) {
                listener.onUpdate(this.member);
            }
        }

        this.member = member;
        videoViewItem.setData(null);
        if (videoView.isHasParent()) {
            ((ViewGroup) videoView.getParent()).removeAllViews();
        }
        videoViewItem.addVideoView(videoView, getItemLayoutParams());
        videoView.setZOrderOnTop(true);
        videoView.setZOrderMediaOverlay(true);

        //        if (isShowVideo()) {
        //            rootView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        //        } else {
        //            rootView.setBackgroundColor(getResources().getColor(R.color.colorClassMainBg));
        //        }
    }

    public ClassMember getMember() {
        return member;
    }

    /**
     * 判断当前共享区域显示类型
     *
     * @return
     */
    public boolean isShowVideo() {
        if (classViewModel == null) {
            return false;
        }
        ScreenDisplay value = classViewModel.getDisplay().getValue();
        if (value == null) {
            return false;
        }
        if (value.getType() != ScreenDisplay.Display.LECTURER) {
            return false;
        }

        return true;
    }


    /**
     * 设置选择监听
     */
    public interface OnWindowCloseListener {
        void onClosed(ClassMember member);

        void onUpdate(ClassMember member);
    }
}
