package com.dwin.DwinTools;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.dwin.DwinTools.helper.PermissionHelper;
import com.dwin.DwinTools.serial.SerialMainMenu;
import com.dwin.DwinTools.util.ChipUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private final String TAG = MainActivity.class.getSimpleName();
    private static final String ACTION_DWIN_TEST = "android.intent.action.dwin_test";

    Button mButtonTest;
    Button mButtonSerial;
    Button mButtonNavigationBar;
    Button mButtonRotation;
    Button mButtonGetChipId;
    Button mButtonReboot;
    Button mButtonShutdown;
    TextView mTextViewChipId;
    boolean mFlag = true;

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

        mButtonTest = findViewById(R.id.btn_test);
        mButtonSerial = findViewById(R.id.btn_start_serial);
        mButtonRotation = findViewById(R.id.btn_rotation);
        mButtonNavigationBar = findViewById(R.id.btn_navigation_bar);
        mButtonGetChipId = findViewById(R.id.btn_get_chipid);
        mTextViewChipId = findViewById(R.id.tv_chipid);
        mButtonReboot = findViewById(R.id.btn_reboot);
        mButtonShutdown = findViewById(R.id.btn_shutdown);

        mButtonTest.setOnClickListener(this);
        mButtonSerial.setOnClickListener(this);
        mButtonRotation.setOnClickListener(this);
        mButtonNavigationBar.setOnClickListener(this);
        mButtonGetChipId.setOnClickListener(this);
        mButtonReboot.setOnClickListener(this);
        mButtonShutdown.setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_test:
                test();
                //NavigationBarStatusBar(this,flat);
                mFlag = !mFlag;
                break;
            case R.id.btn_start_serial:
                Intent intentSerial = new Intent(MainActivity.this, SerialMainMenu.class);
                startActivity(intentSerial);
                break;
            case R.id.btn_rotation:
                Intent intentRotation = new Intent(MainActivity.this, RotationActivity.class);
                startActivity(intentRotation);
                break;
            case R.id.btn_navigation_bar:
                Intent intentNavigation = new Intent(MainActivity.this, NavigationBarActivity.class);
                startActivity(intentNavigation);
                break;
            case R.id.btn_get_chipid:
                //获取Chip ID
                String chipId = ChipUtil.getInstance().getChipId();
                mTextViewChipId.setText("Chip ID : " + chipId);
                break;
            case R.id.btn_reboot:
                Intent srReboot = new Intent("android.intent.action.dwin_reboot");
                sendBroadcast(srReboot);
                break;
            case R.id.btn_shutdown:
                Intent srShutdown = new Intent("android.intent.action.dwin_shutdown");
                sendBroadcast(srShutdown);
                break;
        }
    }

    public void test(){
        Log.v(TAG,"Dwin test sendBroadcast!");
        String degree = "input keyevent 4";
        Intent sr = new Intent("android.intent.action.dwin_shell_command");
//        Intent sr = new Intent("action.SYSTEMUI_FIREFLY_POWEROFF");
//        Intent sr = new Intent("android.intent.action.dwin_test");
        sr.putExtra("message", degree);
        sendBroadcast(sr);
    }

    /**
     * 导航栏，状态栏隐藏
     * @param activity
     */

    public static void NavigationBarStatusBar(Activity activity, boolean hasFocus){
        if (hasFocus && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            View decorView = activity.getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }

    /**
     *
     * @param requestCode 申请权限时填的requestCode
     * @param permissions 申请权限时所需申请的权限集合
     * @param grantResults 对应permission每一项的返回值，代表是否申请成功
     */
    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String[] permissions, int[] grantResults) {
        for (int i = 0; i < permissions.length; i++){
            Log.v(TAG,"permissions[" + i + "] = " + permissions[i]);
            Log.v(TAG,"grantResults[" + i + "] = " + grantResults[i]);
        }

        if (PermissionHelper.isAllPermissionsGranted(permissions, grantResults)) {

        } else {
            for (int i = 0; i < permissions.length; i++) {
                if (Manifest.permission.READ_EXTERNAL_STORAGE.equals(permissions[i])
                        && grantResults[i] == PackageManager.PERMISSION_DENIED) {
                    PermissionHelper.showDeniedPrompt(this);
                    break;
                }

                if (Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[i])
                        && grantResults[i] == PackageManager.PERMISSION_DENIED) {
                    PermissionHelper.showDeniedPrompt(this);
                    break;
                }

                if (Manifest.permission.RECORD_AUDIO.equals(permissions[i])
                        && grantResults[i] == PackageManager.PERMISSION_DENIED) {
                    PermissionHelper.showDeniedPrompt(this);
                    break;
                }
            }
            Log.i(TAG, "<onRequestPermissionsResult> permission denied, finish");
            finish();
        }
    }
}
