package mars.mobile;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;

import static mars.mobile.MainActivity.prefs;


public class splash extends Activity {
	private Handler mHandler = new Handler();
    String TAG="splash";
    private String param1,param2,param3,param4,param5,param6;
    boolean  is_first_install;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Log.d(TAG,"splash activity 실행됨 ");
		//*---------------  처음 설치자를 구분하여 초기화면을 정한다.
		SharedPreferences prf;
        boolean law_agree;
		prf=getSharedPreferences("pref" ,MODE_PRIVATE);
        SharedPreferences.Editor editor = prf.edit();
        law_agree = prf.getBoolean("law_agree",false);
        is_first_install = prf.getBoolean("is_first_install",true);
        Log.d(TAG,"is_first_install 1="+ is_first_install);
      if(is_first_install){
          editor.putBoolean("is_first_install", false);//추가코드
          editor.commit();
          Intent i = new Intent(this, Mainlaw.class);
            Log.d(TAG,"MAIN LAW");
          startActivityForResult(i, 5);

      }else{
          Log.d(TAG,"is_first_install="+ is_first_install);
          Log.d(TAG,"law passed");
         go();
      }



	}

	private  void go(){

        int delay_time = 1500;
        //setContentView(R.layout.splash); //세로  원하는데로 정의하면 된다.

        Log.d(TAG,"go()");


        mHandler.postDelayed(new Runnable() {




            @SuppressLint("MissingPermission")
            @Override
            public void run() {
                // TODO Auto-generated method stub

                if(getIntent()!=null){
                    Log.d(TAG, "pass here");
                    Uri uri = getIntent().getData();
                    if(uri != null){
                        param1=uri.getQueryParameter("param1");
                        param2=uri.getQueryParameter("param2");
                        param5=uri.getQueryParameter("param5");
                        param6=uri.getQueryParameter("param6");
                        Log.d(TAG, uri.toString());
                        Log.d(TAG, "param5="+uri.getQueryParameter("param5"));
                        Log.d(TAG, "pass here3");
                        // Toast.makeText(getApplicationContext(),param1 ,
                        // Toast.LENGTH_SHORT).show();
                        if( uri.getQueryParameter("param_intent_url")!=null) {
                            prefs = getSharedPreferences("pref", getApplicationContext().MODE_PRIVATE);
                            SharedPreferences.Editor edit = prefs.edit();

                            String str = uri.getQueryParameter("param_intent_url");

                            edit.putString("param_intent_url", uri.getQueryParameter("param_intent_url"));
                            edit.commit();
                        }
                    }
                }




                     String Str_MyPhoneNumber,Android_id;
                    Android_id =  Settings.Secure.getString(getContentResolver(),
                            Settings.Secure.ANDROID_ID);

                    SharedPreferences.Editor editor;
                    prefs =getSharedPreferences("pref", Context.MODE_PRIVATE);//
                    editor = prefs.edit();

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

                if(Build.VERSION.SDK_INT<23 && is_first_install == false) {
                    Log.d(TAG, "go SDK_INT<23");
                    Intent intent = new Intent(splash.this, MainActivity.class);
                   startActivity(intent);
                }else {
                    Log.d(TAG, "go permissoin_start");
                    Intent intent = new Intent();
                    intent.setClass(splash.this, permission_start.class);
                    if (param1 != null) {
                        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));
                    startActivity(intent);

                    if(Build.VERSION.SDK_INT<23) {
                        overridePendingTransition(R.anim.appear_from_left, R.anim.disappear_to_right);
                        finish();
                    }
                }
                overridePendingTransition(R.anim.appear_from_left, R.anim.disappear_to_right);
               finish();



            }
            //Do Something
        }, delay_time);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK){
            switch (requestCode){
// MainActivity 에서 요청할 때 보낸 요청 코드 (3000)
                case 5:
                    Log.d(TAG, "onActivityResult agree");
                    String result=data.getStringExtra("agree");
                    go();
                    break;
            }
        }else {
           // go();
            Log.d(TAG, "go else");
            if(Build.VERSION.SDK_INT<23) {
                finish();
            }
        }


     /*
        if (requestCode == 5) {
            if(resultCode == Activity.RESULT_OK){
                Log.d(TAG,"RESULT_OK");
                String result=data.getStringExtra("agree");
                go();
            }else if (resultCode == Activity.RESULT_CANCELED) {
              finish();
              // go();
            }
        }else {
            //go();
            Log.d(TAG,"RESULT_OK2");
        }*/
    }

}