package com.thirtysparks.apple.bot;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;

import java.util.GregorianCalendar;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    ReserveWorker reserveWorker;

    TextView tvMsg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivty_main);

        tvMsg = (TextView)findViewById(R.id.tv_msg);

        reserveWorker = new ReserveWorker();
        goFrontPage();
    }

    private void goFrontPage(){
        tvMsg.setText("Visiting front page");
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... voids) {
                String resultUrl = null;
                try {
                    resultUrl = reserveWorker.visitFirstPage();
                }
                catch(Exception e){
                    e.printStackTrace();
                }

                return resultUrl;
            }

            @Override
            protected void onPostExecute(String resultString) {
                //update the UI

                Log.d(TAG, "Result url is " + resultString);
                boolean isLogin = (resultString.startsWith("https://signin.apple.com/IDMSWebAuth/"));
                if(isLogin){
                    tvMsg.setText("Success. Redirected to login page");
                }
                else{
                    tvMsg.setText("Failed");
                }

                getCaptcha();

            }
        }.execute();
    }


    private void getCaptcha() {
        tvMsg.setText("Getting captcha");
        ImageView captchaImageView = (ImageView)findViewById(R.id.iv_captcha);
        String imageUrl = "https://signin.apple.com/IDMSWebAuth/imageCaptcha/942#" + GregorianCalendar.getInstance().getTime().getTime();
        Glide.with(MainActivity.this).load(imageUrl).into(captchaImageView);
    }
}

@@ -139,16 +139,74 @@
 il.Log;%0A
+import android.view.View;%0Aimport android.widget.EditText;%0A
 import a
@@ -226,24 +226,24 @@
 .ImageView;%0A
-
 import andro
@@ -432,16 +432,140 @@
 ivity%22;%0A
+    private static final String APPLE_ID = %22ENTER_APPLIE_ID%22;%0A    private static final String PASSWORD = %22ENTER_PASSWORD%22;%0A%0A
     Rese
@@ -867,24 +867,237 @@
 veWorker();%0A
+%0A        findViewById(R.id.btn_captcha).setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                goLoginCaptcha();%0A            %7D%0A        %7D);%0A%0A
         goFr
@@ -2115,16 +2115,18 @@
        g
+oG
 etCaptch
@@ -2192,16 +2192,18 @@
 e void g
+oG
 etCaptch
@@ -2463,24 +2463,24 @@
 .getTime();%0A
-
         Glid
@@ -2550,10 +2550,1037 @@
 ;%0A    %7D%0A
+%0A%0A%0A    private void goLoginCaptcha() %7B%0A        tvMsg.setText(%22Submitting captcha%22);%0A%0A        final String captchaInput = ((EditText) findViewById(R.id.et_captcha)).getText().toString();%0A        new AsyncTask%3CVoid, Void, String%3E() %7B%0A            @Override%0A            protected String doInBackground(Void... params)  %7B%0A                String string = null;%0A                try %7B%0A                    string = reserveWorker.loginWithCaptcha(captchaInput, APPLE_ID, PASSWORD);%0A                %7D%0A                catch(Exception e)%7B%0A                    e.printStackTrace();%0A                %7D%0A%0A                return string;%0A            %7D%0A%0A            @Override%0A            protected void onPostExecute(String s) %7B%0A                if (%22https://reserve-hk.apple.com/HK/en_HK/reserve/iPhone?execution=e1s2%22.equals(s)) %7B%0A                    tvMsg.setText(%22Apple ID Login successfully%22);%0A                %7D else %7B%0A                    tvMsg.setText(%22Error: Apple ID Login failed%22);%0A                %7D%0A            %7D%0A        %7D.execute();%0A    %7D%0A
 %7D%0A

Add `goLoginCaptcha` method to `MainActivity`.