package mars.mobile;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.Settings;
import androidx.core.app.ActivityCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.WindowManager;

import static mars.mobile.MainActivity.prefs;

public class permission_start extends AppCompatActivity {

    private static final int REQUEST_CODE = 0;
    //private static final String[] PERMISSIONS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE, Manifest.permission.CAMERA};
    private static final String[] PERMISSIONS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE , Manifest.permission.CAMERA , Manifest.permission.POST_NOTIFICATIONS };
    private PermissionsChecker checker;
    private SharedPreferences.Editor editor;
    private String Str_MyPhoneNumber,Android_id;
    private String TAG="permission_start";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permissions);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        prefs =getSharedPreferences("pref", Context.MODE_PRIVATE);
        editor = prefs.edit();
        checker = new PermissionsChecker(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "kong passx");
        if (checker.lacksPermissions(PERMISSIONS)) {
            Log.d(TAG, "kong pass1");
            startPermissionsActivity();
        } else {

           /* Log.d(TAG, "kong passb");

            Intent get_intent = new Intent(this.getIntent());
            String param1 = get_intent.getStringExtra("order_id");
            String param2 = get_intent.getStringExtra("url");
            String param3 = get_intent.getStringExtra("arg1");
            String param4 = get_intent.getStringExtra("arg2");
            String param5 = get_intent.getStringExtra("param5");
            String param6 = get_intent.getStringExtra("param6");

            Intent intent = new Intent();
            intent.setClass(this, MainActivity.class);
            intent.putExtra("param1", param1);
            intent.putExtra("param2", param2);
            intent.putExtra("param5", param5);
            intent.putExtra("param6", param6);*/

            //	intent.putExtra("appname", getResources().getString(R.string.app_name));
            Intent intent = new Intent();
            intent.setClass(this, MainActivity.class);
            startActivity(intent);

            finish();

        }
    }

    private void startPermissionsActivity() {
        PermissionsActivity.startActivityForResult(this, REQUEST_CODE, PERMISSIONS);
        Log.d(TAG, "kong pass2");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
            finish();
        } else if (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_GRANTED) {
            Log.d(TAG, "kong pass granted");
            TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            if ( ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                 && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED




            ) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }


//            if (telManager.getSimState() != TelephonyManager.SIM_STATE_ABSENT){
//                //the phone has a sim card
//                Str_MyPhoneNumber = telManager.getLine1Number();
//                Str_MyPhoneNumber=   Str_MyPhoneNumber.replace("+82", "0");
//            } else {
//                //베가 R3는 와이파이전용폰이라서 전화번호 대신 다른것을 집어 넣음
//                Str_MyPhoneNumber = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
//            }
            Str_MyPhoneNumber = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
            Android_id =  Settings.Secure.getString(this.getContentResolver(),
                    Settings.Secure.ANDROID_ID);

            Log.d(TAG, "permission_start "+Str_MyPhoneNumber);
            Android_id = Settings.Secure.getString(this.getContentResolver(),
                    Settings.Secure.ANDROID_ID);
            Log.d(TAG, Android_id);//  

            editor.putString("Str_MyPhoneNumber", Str_MyPhoneNumber);//추가코드//다시 초기화 한다.
            editor.putString("Android_id", Android_id);//추가코드//다시 초기화 한다.
          //  editor.putBoolean("token_refreshed",true);// 앱이 재시작 할때 토큰을 저장하게 시킨다
            editor.commit();

            Log.d(TAG, "permission_start geted Str_MyPhoneNumber"+prefs.getString("Str_MyPhoneNumber",""));
            Log.d(TAG, "permission_start geted Android_id"+prefs.getString("Android_id",""));

            Intent intent = new Intent();
            intent.setClass(this, MainActivity.class);
            startActivity(intent);
            finish();

        }
    }
}
