package com.huawei.pushpuresdkdemo;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.github.faucamp.simplertmp.RtmpHandler;
import com.huawei.publishsdk.HWCameraSurfaceView;
import com.huawei.publishsdk.HWVideoFrame;
import com.huawei.publishsdk.HwEncodeHandler;
import com.huawei.publishsdk.HwPublisher;
import com.huawei.publishsdk.HwRecordHandler;

import java.io.IOException;
import java.net.SocketException;

import static com.huawei.publishsdk.HwPublisher.ARGB;
import static com.huawei.publishsdk.HwPublisher.NV21;

public class MainActivity2 extends AppCompatActivity implements HwEncodeHandler.HwEncodeListener, RtmpHandler.RtmpListener, HwRecordHandler.HwRecordListener {
    private static final String TAG = "MainActivity2";
    private String rtmpUrl = "rtmp://kitpush.hwcloudlive.com/live1/sdkdemo11";
    private HWCameraSurfaceView mCameraView;
    private HwPublisher mPublisher;

    private Button btnPublish;
    private Button btnSwitchCamera;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_main2);

        btnPublish = (Button) findViewById(R.id.publish);
        btnSwitchCamera = (Button) findViewById(R.id.swCam);
        mCameraView = (HWCameraSurfaceView) findViewById(R.id.glsurfaceview_camera2);

        mPublisher = new HwPublisher(this, mCameraView,new HwPublisher.HWPreviewCallbackNV21(){

            @Override
            public void onGetNV21Frame(byte[] data, int width, int height,int rotation) {
//                HWVideoFrame videoFrame = new HWVideoFrame();
//                videoFrame.data=data;
//                videoFrame.width =width;
//                videoFrame.height=height;
//                videoFrame.type = NV21;
//                videoFrame.rotation = rotation;
//                mPublisher.pushExternVideoFrame(videoFrame);
            }
        },false);
        mPublisher.setEncodeHandler(new HwEncodeHandler(this));
        mPublisher.setRtmpHandler(new RtmpHandler(this));
        mPublisher.setRecordHandler(new HwRecordHandler(this));

        mPublisher.setOutputResolution(720, 1280);
        mPublisher.setVideoHDMode();
        mPublisher.setVfps(15);
//        mPublisher.switchToSoftEncoder();

        btnPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (btnPublish.getText().toString().contentEquals("publish")) {
                    mPublisher.startAudioRecord();
                    mPublisher.startPublish(rtmpUrl);
                    btnPublish.setText("stop");
                } else if (btnPublish.getText().toString().contentEquals("stop")) {
                    mPublisher.stopAudioRecord();
                    mPublisher.stopPublish();
                    btnPublish.setText("publish");
                }
            }
        });

        btnSwitchCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPublisher.switchCameraFace((mPublisher.getCamraId() + 1) % Camera.getNumberOfCameras());
            }
        });


    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mPublisher.stopPublish();
        mPublisher.stopRecord();
        mPublisher.setScreenOrientation(newConfig.orientation);
        if (btnPublish.getText().toString().contentEquals("stop")) {
            mPublisher.startPublish(rtmpUrl);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }
    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPublisher.stopPublish();
        mPublisher.stopRecord();
        mPublisher.stopAudioRecord();
        mPublisher.stopCamera();
        mPublisher.release();
    }


    @Override
    public void onNetworkWeak() {

    }

    @Override
    public void onNetworkResume() {

    }

    @Override
    public void onEncodeIllegalArgumentException(IllegalArgumentException e) {
        Log.e(TAG, "onEncodeIllegalArgumentException: "+e );
    }

    @Override
    public void onRtmpConnecting(String msg) {

    }

    @Override
    public void onRtmpConnected(String msg) {
        Log.e(TAG, "onRtmpConnected: " );
    }

    @Override
    public void onRtmpVideoStreaming() {

    }

    @Override
    public void onRtmpAudioStreaming() {

    }

    @Override
    public void onRtmpStopped() {

    }

    @Override
    public void onRtmpDisconnected() {

    }

    @Override
    public void onRtmpVideoFpsChanged(double fps) {

    }

    @Override
    public void onRtmpVideoBitrateChanged(double bitrate) {

    }

    @Override
    public void onRtmpAudioBitrateChanged(double bitrate) {

    }

    @Override
    public void onRtmpSocketException(SocketException e) {
        handleException(e);
    }

    @Override
    public void onRtmpIOException(IOException e) {
        handleException(e);
    }

    @Override
    public void onRtmpIllegalArgumentException(IllegalArgumentException e) {
        handleException(e);
    }

    @Override
    public void onRtmpIllegalStateException(IllegalStateException e) {
        handleException(e);
    }

    @Override
    public void onRecordPause() {

    }

    @Override
    public void onRecordResume() {

    }

    @Override
    public void onRecordStarted(String msg) {

    }

    @Override
    public void onRecordFinished(String msg) {

    }

    @Override
    public void onRecordIllegalArgumentException(IllegalArgumentException e) {

    }

    @Override
    public void onRecordIOException(IOException e) {

    }

    private void handleException(Exception e) {
        try {
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            mPublisher.stopPublish();
            mPublisher.stopRecord();

            btnPublish.setText("publish");
        } catch (Exception e1) {
            //
        }
    }
}
