package com.huawei.interactivebroadcastingwithcdnstreaming;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.PermissionChecker;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.huawei.interactivebroadcastingwithcdnstreaming.screenlive.app.AudienceActivity;
import com.huawei.interactivebroadcastingwithcdnstreaming.screenlive.app.ScreenLiveActivity;
import com.huawei.interactivebroadcastingwithcdnstreaming.screenlive.app.ScreenLiveMainActivity;

public class MainActivity extends AppCompatActivity {
    public static final int CLIENT_ROLE_BROADCASTER = 1;
    public static final int CLIENT_ROLE_AUDIENCE = 2;

    private String mAppid;
    private String mTempToken;
    private String mChannelName;
    private String mPublishUrl;
    private String mUid;
    private int role = CLIENT_ROLE_BROADCASTER;

    private EditText etAppid;
    private EditText etTempToken;
    private EditText etChannelName;
    private EditText etUrl;
    private EditText etUid;
    private RadioGroup radioGroupRole;

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

        etAppid = findViewById(R.id.et_appid);
//        etAppid.setText("b1288f16ddb0442dadb3989cc0f721da");
        etTempToken = findViewById(R.id.et_temp_token);
        Log.e("etTempToken", "onCreate etTempToken: "+etTempToken );
        etChannelName = findViewById(R.id.et_channel_name);
        etUrl = findViewById(R.id.et_publish_url);
        etUid = findViewById(R.id.et_uid);
        radioGroupRole = (RadioGroup) findViewById(R.id.rg_role);
        radioGroupRole.setOnCheckedChangeListener(radioGrouplisten);

    }

    //RadioGroup控件的初始化
    private RadioGroup.OnCheckedChangeListener radioGrouplisten = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int id = group.getCheckedRadioButtonId();
            switch (group.getCheckedRadioButtonId()) {
                case R.id.rb_broadcast:
                    role = CLIENT_ROLE_BROADCASTER;
                    //Toast.makeText(PrintSettingActivity.this, orientation.toString(), Toast.LENGTH_SHORT).show();
                    break;
                case R.id.rb_audience:
                    role = CLIENT_ROLE_AUDIENCE;
                    //Toast.makeText(PrintSettingActivity.this, orientation.toString(), Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    public void onJoinClicked(View v) {
        mAppid = etAppid.getText().toString();
        mTempToken = etTempToken.getText().toString();
        mChannelName = etChannelName.getText().toString();
        mPublishUrl = etUrl.getText().toString();
        mUid = etUid.getText().toString();

        if (TextUtils.isEmpty(mTempToken)) {
            mTempToken=null;
        }

        if (TextUtils.isEmpty(mAppid)) {
            Toast.makeText(this, "please input a mAppid", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(mChannelName)) {
            Toast.makeText(this, "please input a channel name", Toast.LENGTH_SHORT).show();
            return;
        }

        if (checkSelfPermissions()) {
            forwardToMain();
        }
    }

    public void onScreenClicked(View v) {
        mAppid = etAppid.getText().toString();
        mTempToken = etTempToken.getText().toString();
        mChannelName = etChannelName.getText().toString();
        mPublishUrl = etUrl.getText().toString();
        mUid = etUid.getText().toString();

        if (TextUtils.isEmpty(mTempToken)) {
            mTempToken=null;
        }

        if (TextUtils.isEmpty(mAppid)) {
            Toast.makeText(this, "please input a mAppid", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(mChannelName)) {
            Toast.makeText(this, "please input a channel name", Toast.LENGTH_SHORT).show();
            return;
        }

        if (checkSelfPermissions()) {
            forwardToScreenMain();
        }
    }

    public void onReadme(View v){
        Intent i = new Intent(this, readmeActivity.class);
        startActivity(i);
    }

    private static final int PERMISSION_REQ_ID = 1024;

    @TargetApi(Build.VERSION_CODES.M)
    private void askPermission() {
        requestPermissions(new String[]{
                        Manifest.permission.CAMERA,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE},
                PERMISSION_REQ_ID);
    }

    private boolean checkSelfPermissions() {
        return checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID) &&
                checkSelfPermission(Manifest.permission.CAMERA, PERMISSION_REQ_ID) &&
                checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_REQ_ID);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQ_ID) {
            for (int g : grantResults) {
                if (g != PermissionChecker.PERMISSION_GRANTED) {
                    return;
                }
            }
        }
    }

    public boolean checkSelfPermission(String permission, int requestCode) {
        if (ContextCompat.checkSelfPermission(this,
                permission)
                != PackageManager.PERMISSION_GRANTED) {

            askPermission();
            return false;
        }
        return true;
    }

    private void forwardToMain() {
        Intent i = new Intent(this, VideoActivity.class);
        i.putExtra("CHANNEL", mChannelName);
        i.putExtra("URL", mPublishUrl);
        i.putExtra("APPID", mAppid);
        i.putExtra("TOKEN", mTempToken);
        i.putExtra("UID", mUid);
        i.putExtra("ROLE",role);
        startActivity(i);
    }

    private void forwardToScreenMain() {
        Intent i = new Intent();
        i.putExtra("CHANNEL", mChannelName);
        i.putExtra("URL", mPublishUrl);
        i.putExtra("APPID", mAppid);
        i.putExtra("TOKEN", mTempToken);
        i.putExtra("UID", mUid);
        i.putExtra("ROLE",role);
        if (role == CLIENT_ROLE_AUDIENCE){
            i.setClass(this,AudienceActivity.class);
        }else {
            i.setClass(this, ScreenLiveActivity.class);
        }
        startActivity(i);
    }
}
