package com.wdkl.core.voip;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wdkl.ncs.android.middleware.common.Constant;
import com.wdkl.skywebrtc.CallSession;
import com.wdkl.skywebrtc.EnumType;
import com.wdkl.skywebrtc.SkyEngineKit;
import com.wdkl.webrtc.R;

/**
 * Created by dds on 2020/5/24.
 * ddssingsong@163.com
 */
public class FragmentMeeting extends Fragment implements CallSession.CallSessionCallback, View.OnClickListener {
    private SkyEngineKit gEngineKit;
    private CallMultiActivity activity;
    private NineGridView grid_view;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity = (CallMultiActivity) getActivity();
        if (activity != null) {

        }

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_meeting, container, false);
        initView(view);
        init();
        return view;
    }

    private void initView(View view) {
        grid_view = view.findViewById(R.id.grid_view);

    }


    private void init() {
        gEngineKit = activity.getEngineKit();
    }

    @Override
    public void onClick(View v) {

    }


    @Override
    public void didCallEndWithReason(EnumType.CallEndReason var1) {

    }

    @Override
    public void didChangeState(EnumType.CallState var1) {

    }

    @Override
    public void didChangeMode(boolean isAudioOnly) {

    }

    @Override
    public void didCreateLocalVideoTrack() {
        Log.d("wzlll", "didCreateLocalVideoTrack");
        View surfaceView = gEngineKit.getCurrentSession().setupLocalVideo(true);
        if (surfaceView != null) {
            CallSession callSession = SkyEngineKit.Instance().getCurrentSession();
            grid_view.addView(callSession.mMyId, surfaceView);


        }

    }

    @Override
    public void didReceiveRemoteVideoTrack(String userId) {
        CallSession callSession = SkyEngineKit.Instance().getCurrentSession();
        Log.d("wzlll", "didReceiveRemoteVideoTrack  user: " + userId + ", myid: " + callSession.mMyId + ", host id: " + Constant.SIP_VISIT_HOST_ID);
        //护士主机画面不加入
        if (!Constant.SIP_VISIT_HOST_ID.equals(userId)) {
            View surfaceView = gEngineKit.getCurrentSession().setupRemoteVideo(userId, true);
            if (surfaceView != null) {
                grid_view.addView(userId, surfaceView);
            }
        }
    }

    @Override
    public void didUserLeave(String userId) {
        grid_view.removeView(userId);
    }

    @Override
    public void didError(String error) {

    }

    @Override
    public void didDisconnected(String userId) {

    }


}
