package com.wdkl.core.voip;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.wdkl.core.base.BaseActivity;
import com.wdkl.ncs.android.middleware.common.Constant;
import com.wdkl.ncs.android.middleware.common.MessageEvent;
import com.wdkl.ncs.android.middleware.tcp.channel.DeviceChannel;
import com.wdkl.ncs.android.middleware.tcp.channel.VideoUtil;
import com.wdkl.ncs.android.middleware.tcp.dto.TcpModel;
import com.wdkl.ncs.android.middleware.tcp.enums.TcpAction;
import com.wdkl.ncs.android.middleware.tcp.enums.TcpType;
import com.wdkl.permission.Permissions;
import com.wdkl.skywebrtc.CallSession;
import com.wdkl.skywebrtc.EnumType;
import com.wdkl.skywebrtc.SkyEngineKit;
import com.wdkl.skywebrtc.except.NotInitializedException;
import com.wdkl.webrtc.R;

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

import java.util.UUID;

/**
 * Created by dds on 2018/7/26.
 * 多人通话界面
 */
public class CallMultiActivity extends BaseActivity implements CallSession.CallSessionCallback, View.OnClickListener {
    private SkyEngineKit gEngineKit;
    private final Handler handler = new Handler(Looper.getMainLooper());
    private ImageView meetingHangupImageView;
    private CallSession.CallSessionCallback currentFragment;
    public static final String EXTRA_MO = "isOutGoing";
    private boolean isOutgoing;

    private boolean leave;

    public static void openActivity(Activity activity, String room, boolean isOutgoing) {
        Intent intent = new Intent(activity, CallMultiActivity.class);
        intent.putExtra("room", room);
        intent.putExtra(EXTRA_MO, isOutgoing);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        getWindow().getDecorView().setSystemUiVisibility(getSystemUiVisibility());

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        setContentView(R.layout.activity_multi_call);
        initView();
        initListener();
        initData();
    }


    private void initView() {
        meetingHangupImageView = findViewById(R.id.meetingHangupImageView);
        Fragment fragment = new FragmentMeeting();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .add(R.id.meeting_container, fragment)
                .commit();
        currentFragment = (CallSession.CallSessionCallback) fragment;
    }

    private void initListener() {
        meetingHangupImageView.setOnClickListener(this);
    }

    private void initData() {
        Intent intent = getIntent();
        String room = intent.getStringExtra("room");
        isOutgoing = intent.getBooleanExtra(EXTRA_MO, false);
        try {
            gEngineKit = SkyEngineKit.Instance();
        } catch (NotInitializedException e) {
            finish();
        }
        String[] per = new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA};
        Permissions.request(this, per, integer -> {
            if (integer == 0) {
                // 权限同意
                init(room, isOutgoing);
            } else {
                // 权限拒绝
                CallMultiActivity.this.finish();
            }
        });


    }

    private void init(String room, boolean isOutgoing) {
        SkyEngineKit.init(new VoipEvent());
        if (isOutgoing) {
            // 创建一个房间并进入
            gEngineKit.createAndJoinRoom(this, room);
        } else {
            // 加入房间
            gEngineKit.joinRoom(this, room);
        }


        CallSession session = gEngineKit.getCurrentSession();
        if (session == null) {
            this.finish();
        } else {
            session.setSessionCallback(this);
            session.toggleSpeaker(true);
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Constant.CALL_STATE = Constant.CALL_STANDBY;
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    public SkyEngineKit getEngineKit() {
        return gEngineKit;
    }


    @TargetApi(19)
    private static int getSystemUiVisibility() {
        int flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            flags |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        }
        return flags;
    }


    //-------------------------------------------------回调相关------------------------------------
    @Override
    public void didCallEndWithReason(EnumType.CallEndReason var1) {
        finish();
    }

    @Override
    public void didChangeState(EnumType.CallState callState) {
        handler.post(() -> currentFragment.didChangeState(callState));
    }

    @Override
    public void didChangeMode(boolean var1) {
        handler.post(() -> currentFragment.didChangeMode(var1));
    }

    @Override
    public void didCreateLocalVideoTrack() {
        handler.post(() -> currentFragment.didCreateLocalVideoTrack());
    }

    @Override
    public void didReceiveRemoteVideoTrack(String userId) {
        handler.post(() -> currentFragment.didReceiveRemoteVideoTrack(userId));
    }

    @Override
    public void didUserLeave(String userId) {
        //handler.post(() -> currentFragment.didUserLeave(userId));

        if (!leave) {
            leave = true;
            Constant.CALL_STATE = Constant.CALL_STANDBY;
            SkyEngineKit.Instance().leaveRoom();
        }
        finish();
    }

    @Override
    public void didError(String var1) {
        finish();
    }

    @Override
    public void didDisconnected(String userId) {

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.meetingHangupImageView) {
            handleHangup();
        }

        /*switch (v.getId()) {
            case R.id.meetingHangupImageView:
                handleHangup();
                break;
        }*/

    }

    // 处理挂断事件
    private void handleHangup() {
        if (!leave) {
            leave = true;
            Constant.CALL_STATE = Constant.CALL_STANDBY;
            VideoUtil.handoffVideoCall(Constant.DEVICE_ID, Constant.fromId, Constant.interactionId);
            SkyEngineKit.Instance().leaveRoom();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent) {
        if (messageEvent.getType() == Constant.EVENT_TCP_MSG){
            if (messageEvent.getMessage() instanceof TcpModel) {
                TcpModel tcpModel = (TcpModel) messageEvent.getMessage();
                if (tcpModel.getType() == TcpType.VIDEO) {
                    if (tcpModel.getAction() == TcpAction.VideoAction.HANDOFF && !leave) {
                        leave = true;
                        Constant.CALL_STATE = Constant.CALL_STANDBY;
                        SkyEngineKit.Instance().leaveRoom();
                    }
                }
            }
        }
    }
}
