package mars.mobile;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by cho on 2015-11-12.
 */



public  class MyAsyncTask extends AsyncTask< String , Integer , String> {

    String url,selected_webview,regId,PhoneNumber,index;
    private Context mContext;
    private String param_intent_url;
    public static SharedPreferences prefs;

    public MyAsyncTask (Context context){
        mContext = context;
    }

    @Override
    protected String doInBackground(String... params) {// 안보이는것 처리
        // TODO Auto-generated method stub
        url=params[0];
        selected_webview=params[1];
      //  return regId;
        return null;
    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        // Android ID 가져오기
        String Android_id = Settings.Secure.getString(mContext.getContentResolver(),
                Settings.Secure.ANDROID_ID);

        // 현재 앱 버전 가져오기
        int currentVersion = MainActivity.getAppVersion(mContext);
        String str_currentVersion = String.valueOf(currentVersion);

        // paik param_intent_url 가져오기
        //param_intent_url = MainActivity.prefs.getString("param_intent_url", null);

        // token 값을 SharedPreferences에서 가져오기
        SharedPreferences prefs = mContext.getSharedPreferences("pref", Context.MODE_PRIVATE);
        String token = prefs.getString("token", null);

        // SharedPreferences Editor를 가져옵니다.
        SharedPreferences.Editor editor2 = MainActivity.prefs.edit();

        // param_intent_url 값을 null로 설정합니다.
        //editor2.putString("param_intent_url", null);

        // 변경 사항을 적용합니다.
        editor2.apply(); // 또는 editor.commit();


        // URL 및 웹뷰 초기화 확인
        if (MainActivity.webView != null) {
            MainActivity.webView.getSettings().setJavaScriptEnabled(true); // JavaScript 활성화


            if (MainActivity.weblink == null ){

                String finalUrl = url + "?regid=" + token + "&Android_id=" + Android_id + "&build_number=" + str_currentVersion + "&connect=android";
                MainActivity.loadUrlWithHistory(finalUrl);

            }
            else {

                if ("webView".equals(selected_webview)) {  // 문자열 비교 수정
                    String urlToLoad = MainActivity.weblink != null ? MainActivity.weblink : param_intent_url;

                    if (urlToLoad != null) {
                        String finalUrl = urlToLoad; // 파라미터 없이 순수 URL만 사용

                        // loadUrlWithHistory 메서드 사용하여 히스토리 관리
                        MainActivity.loadUrlWithHistory(finalUrl);
                    } else {
                        String finalUrl = url + "?regid=" + token + "&Android_id=" + Android_id + "&build_number=" + str_currentVersion + "&connect=android";
                        MainActivity.loadUrlWithHistory(finalUrl);
                    }
                }
            }
        } else {
            Log.e("WebView Error", "WebView is not initialized");
        }
    }





//    @Override
//    protected void onPostExecute(String result) {// 보이는거 처리
//        // TODO Auto-generated method stub
//        super.onPostExecute(result);
//        String Android_id =  Settings.Secure.getString(mContext.getContentResolver(),
//                Settings.Secure.ANDROID_ID);
//
//        int currentVersion = MainActivity.getAppVersion(mContext);
//        String str_currentVersion = String.valueOf(currentVersion);
//        param_intent_url =  MainActivity.prefs.getString("param_intent_url", null);
//       // Toast.makeText(mContext.getApplicationContext(),  "'111."+param_intent_url,	Toast.LENGTH_SHORT).show();
//        SharedPreferences.Editor
//                editor = MainActivity.prefs.edit();
//
//        Android_id = md5.testMD5(Android_id);
//        prefs = mContext.getSharedPreferences("pref", Context.MODE_PRIVATE);
//        String token = prefs.getString("token", null);//추가된 코드
//        if(selected_webview=="webView")if(selected_webview=="webView") {
//
//            if(MainActivity.weblink !=null) {
//                MainActivity.webView.loadUrl(MainActivity.weblink + "&connect=android&Android_id=" + Android_id +  "&regid=" + token + "&build_number=" + str_currentVersion);
//                editor.putString("weblink", null);
//                editor.commit();
//            }
//            else {
//                if(param_intent_url!=null) {
//
//                    MainActivity.webView.loadUrl(param_intent_url + "&connect=android&Android_id=" + Android_id + "&regid=" + token + "&build_number=" + str_currentVersion);
//                    editor.putString("param_intent_url", null);
//                    editor.commit();
//                }
//                else {
//              //      Toast.makeText(mContext.getApplicationContext(),  "'뒤로'버튼 한번 더 누르시면 종료됩니다.",	Toast.LENGTH_SHORT).show();
//                    MainActivity.webView.loadUrl(url + "?regid=" + token +"&Android_id=" + Android_id + "&build_number=" + str_currentVersion +"&connect=android");
//                }
//                Log.d("bb", url + "?regid=" + token + "&Android_id=" + Android_id + "&connect=android&build_number=" + str_currentVersion);
//
//
//            }
//        }       // MainActivity.webView.loadUrl(url);
//
//
//        else if(selected_webview=="compress_img") {
//          //  picture_activity.upload_scaled.compress(Bitmap.CompressFormat.PNG, 10, picture_activity.ostream);
//          //  picture_activity.ostream.close();
//          //  dialog.dismiss();
//        }
//        else {
//            MainActivity.webView_menu.loadUrl(url);
//        }
//
//    }

}