package com.wdkl.core.voip;

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


import com.wdkl.core.base.BaseActivity;
import com.wdkl.ncs.android.middleware.common.Constant;
import com.wdkl.ncs.android.middleware.tcp.channel.VideoUtil;
import com.wdkl.ncs.android.middleware.tcp.channel.VoiceUtil;
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 java.util.UUID;

import serialporttest.utils.SerialPortUtil;


/**
 * Created by dds on 2018/7/26.
 * 单人通话界面
 */
public class CallSingleActivity extends BaseActivity implements CallSession.CallSessionCallback {

    public static final String EXTRA_TARGET = "targetId";
    public static final String EXTRA_MO = "isOutGoing";
    public static final String EXTRA_AUDIO_ONLY = "audioOnly";
    public static final String EXTRA_USER_NAME = "userName";
    public static final String EXTRA_FROM_FLOATING_VIEW = "fromFloatingView";
    private static final String TAG = "CallSingleActivity";

    private Handler handler = new Handler(Looper.getMainLooper());
    private boolean isOutgoing;
    private String targetId;
    boolean isAudioOnly;
    private boolean isFromFloatingView;

    private SkyEngineKit gEngineKit;

    private SingleCallFragment currentFragment;
    private String room;

    public static Intent getCallIntent(Context context, String targetId, boolean isOutgoing, String inviteUserName,
                                       boolean isAudioOnly, boolean isClearTop) {
        Intent voip = new Intent(context, CallSingleActivity.class);
        voip.putExtra(CallSingleActivity.EXTRA_MO, isOutgoing);
        voip.putExtra(CallSingleActivity.EXTRA_TARGET, targetId);
        voip.putExtra(CallSingleActivity.EXTRA_USER_NAME, inviteUserName);
        voip.putExtra(CallSingleActivity.EXTRA_AUDIO_ONLY, isAudioOnly);
        voip.putExtra(CallSingleActivity.EXTRA_FROM_FLOATING_VIEW, false);
        //if (isClearTop) {
        //    voip.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //}
        return voip;
    }


    public static void openActivity(Context context, String targetId, boolean isOutgoing, String inviteUserName,
                                    boolean isAudioOnly, boolean isClearTop) {
        Intent intent = getCallIntent(context, targetId, isOutgoing, inviteUserName, isAudioOnly, isClearTop);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBarOrScreenStatus(this);
        setContentView(R.layout.activity_single_call);

        try {
            gEngineKit = SkyEngineKit.Instance();
        } catch (NotInitializedException e) {
            SkyEngineKit.init(new VoipEvent()); //重新初始化
            try {
                gEngineKit = SkyEngineKit.Instance();
            } catch (NotInitializedException ex) {
                finish();
            }
        }
        final Intent intent = getIntent();
        targetId = intent.getStringExtra(EXTRA_TARGET);
        isFromFloatingView = intent.getBooleanExtra(EXTRA_FROM_FLOATING_VIEW, false);
        isOutgoing = intent.getBooleanExtra(EXTRA_MO, false);
        isAudioOnly = intent.getBooleanExtra(EXTRA_AUDIO_ONLY, false);

        init(targetId, isOutgoing, isAudioOnly);
    }

    @Override
    public void onBackPressed() {
        //通话时不能按返回键，跟微信同现象，只能挂断结束或者接听
//        super.onBackPressed();
//        if (currentFragment != null) {
//            if (currentFragment instanceof FragmentAudio) {
//                ((FragmentAudio) currentFragment).onBackPressed();
//            } else if (currentFragment instanceof FragmentVideo) {
//                ((FragmentVideo) currentFragment).onBackPressed();
//            }
//        }

    }

    private void init(String targetId, boolean outgoing, boolean audioOnly) {
        SingleCallFragment fragment;
        if (audioOnly) {
            fragment = new FragmentAudio();
        } else {
            fragment = new FragmentVideo();
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        currentFragment = fragment;
        fragmentManager.beginTransaction()
                    .replace(android.R.id.content, fragment)
                    .commit();

        if (outgoing) {
            // 创建会话
            room = UUID.randomUUID().toString() + System.currentTimeMillis();
            boolean b = gEngineKit.startOutCall(getApplicationContext(), room, targetId, audioOnly);
            if (!b) {
                finish();
                return;
            }
            CallSession session = gEngineKit.getCurrentSession();
            if (session == null) {
                finish();
            } else {
                session.setSessionCallback(this);
                session.toggleSpeaker(true);
            }
        } else {
            CallSession session = gEngineKit.getCurrentSession();
            if (session == null) {
                finish();
            } else {
                Log.d(TAG, "session = " + session + "; session.getState() = " + session.getState());
                session.setSessionCallback(this);
            }
        }

    }


    public SkyEngineKit getEngineKit() {
        return gEngineKit;
    }

    public boolean isOutgoing() {
        return isOutgoing;
    }


    public boolean isFromFloatingView() {
        return isFromFloatingView;
    }


    // 切换到语音通话
    public void switchAudio() {
        //init(targetId, isOutgoing, true, true);
    }

    public String getRoomId() {
        return room;
    }

    // ======================================界面回调================================
    @Override
    public void didCallEndWithReason(EnumType.CallEndReason reason) {
        //App.getInstance().setOtherUserId("0");
        //交给fragment去finish
//        finish();
        handler.post(() -> currentFragment.didCallEndWithReason(reason));
    }

    @Override
    public void didChangeState(EnumType.CallState callState) {
        if (callState == EnumType.CallState.Connected) {
            isOutgoing = false;
        }
        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));
    }

    @Override
    public void didError(String var1) {
        handler.post(() -> currentFragment.didError(var1));
//        finish();
    }

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


    // ========================================================================================

    private boolean checkOverlayPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SettingsCompat.setDrawOverlays(this, true);
            if (!SettingsCompat.canDrawOverlays(this)) {
                Toast.makeText(this, "需要悬浮窗权限", Toast.LENGTH_LONG).show();
                SettingsCompat.manageDrawOverlays(this);
                return false;
            }
        }
        return true;
    }


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

    /**
     * 设置状态栏透明
     */
    @TargetApi(19)
    public void setStatusBarOrScreenStatus(Activity activity) {
        Window window = activity.getWindow();
        //全屏+锁屏+常亮显示
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        window.getDecorView().setSystemUiVisibility(getSystemUiVisibility());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.layoutInDisplayCutoutMode =
                    WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            window.setAttributes(layoutParams);
        }
        // 5.0以上系统状态栏透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //清除透明状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //设置状态栏颜色必须添加
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);//设置透明
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //19
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        /*if (Constant.VISITING) {
            VideoUtil.handoffVideoCall(Constant.DEVICE_ID, Constant.fromId, Constant.interactionId);
        } else {
            VoiceUtil.handoffAudioCall(Constant.DEVICE_ID, Constant.fromId, Constant.interactionId);
        }
        Constant.VISITING = false;*/

        Constant.CALL_STATE = Constant.CALL_STANDBY;
        SerialPortUtil.getInstance().sendCommand(SerialPortUtil.CALL_STATUS , "0", "F");
    }
}
