package com.kii.example.sync;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;

import org.json.JSONException;

import com.kii.cloud.storage.EasyClient;
import com.kii.cloud.storage.dataType.KiiUser;
import com.kii.cloud.storage.manager.AuthManager;
import com.kii.cloud.storage.response.CloudExecutionException;
import com.kii.mobilesdk.bridge.KiiUMInfo;
import com.kii.sync.KiiClient;
import com.kii.sync.KiiFile;
import com.kii.sync.KiiFileUtil;
import com.kii.sync.SyncMsg;
import com.kii.sync.utils.Base64;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class HelloSyncActivity extends Activity {
    KiiClient kiiClient;
    AuthManager authMan;
    Handler myHandler;
    String userName = "testUser";
    public static final String appId = "c42a57d0";
    public static final String appKey = "224b6595df9387530feb6f696a51c658";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        kiiClient = KiiClient.getInstance(this.getApplicationContext());
        kiiClient.setServerBaseUrl("http://dev-usergrid.kii.com");

        myHandler = new Handler();
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                new Thread(new Runnable() {
                    public void run() {
                        createUser();
                        myHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                new Thread(new Runnable() {
                                    public void run() {
                                        uploadFile();
                                    }
                                }).start();
                            }
                        });
                    }
                }).start();
            }
        });
    }

    private void createUser() {
        // Set Application ID and Application Key.
        EasyClient.start(this, appId, appKey);
        EasyClient.getInstance().setBaseURL("http://dev-usergrid.kii.com:12110");
        authMan = EasyClient.getUserManager();
        KiiUser kUser = new KiiUser();
        // Create User by communicating KiiCloud with User Manager SDK.
        try {
            userName = userName + Long.toString(System.currentTimeMillis());
            kUser.setUsername(userName);
            authMan.createUser(kUser, "1234");
            authMan.login(userName, "1234");
        } catch (CloudExecutionException e) {
            e.printStackTrace();
            throw new RuntimeException("Create user Failed!");
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Create user Failed!");
        } catch (JSONException e) {
            e.printStackTrace();
            throw new RuntimeException("Create user Failed!");
        }
        KiiUMInfo umInfo = new KiiUMInfo(this, userName, "1234", "KII_ID", userName);
        // Pass the User Information to Sync SDK.
        kiiClient.setKiiUMInfo(umInfo);
    }

    private void uploadFile() {
        try {
            kiiClient.initSync();
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException("Sync Failed!");
        } catch (InstantiationException e) {
            e.printStackTrace();
            throw new RuntimeException("Sync Failed!");
        }
        File txtFile = createFile();
        KiiFile kFile = KiiFileUtil.createKiiFileFromFile(txtFile.getAbsolutePath());
        int ret = kiiClient.upload(kFile);
        if (ret != SyncMsg.OK) {
            throw new RuntimeException("Sync Failed! code: " + ret);
        }
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                updateMain();

            }
        });
    }

    private File createFile() {
        File f = new File(Environment.getExternalStorageDirectory(),
                "hello.txt");
        try {
            f.createNewFile();
            PrintWriter p = new PrintWriter(f);
            p.write("Hello, Sync");
            p.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return f;
    }

    private void updateMain() {
        TextView linkURL = (TextView) HelloSyncActivity.this.findViewById(R.id.url_link);
        MovementMethod method = LinkMovementMethod.getInstance();
        linkURL.setMovementMethod(method);

        linkURL.setText(getLink());
        linkURL.setVisibility(View.VISIBLE);

        EditText userNameET = (EditText)HelloSyncActivity.this.findViewById(R.id.username_disp);
        userNameET.setText(userName);
        userNameET.setVisibility(View.VISIBLE);
    }

    private CharSequence getLink() {
        Uri.Builder b = new Uri.Builder();
        Log.v("HelloSync", Base64.encodeToString((appId+":"+appKey).getBytes(), Base64.DEFAULT));
        String lUrl = b.scheme("http")
                .authority("dev-usergrid.kii.com")
                .appendQueryParameter(
                        "app",
                        Base64.encodeToString(
                                (appId + ":" + appKey).getBytes(),
                                Base64.DEFAULT)).build().toString();
        String htmlLink = "<a href=\"" + lUrl + "\">Sync Success! on Web UI you can see the data uploaded!\nPlease copy user name bellow before click.</a>";
        CharSequence seq = Html.fromHtml(htmlLink);
        return seq;
    }
}
@@ -1798,433 +1798,57 @@
     
-myHandler.post(new Runnable() %7B%0A                            @Override%0A                            public void run() %7B%0A                                new Thread(new Runnable() %7B%0A                                    public void run() %7B%0A                                        uploadFile();%0A                                    %7D%0A                                %7D).start();%0A                            %7D%0A                        %7D
+uploadFile();%0A                        updateMain(
 );%0A 
@@ -3758,159 +3758,8 @@
   %7D%0A
-        myHandler.post(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                updateMain();%0A%0A            %7D%0A        %7D);%0A
     
@@ -4251,24 +4251,126 @@
 ateMain() %7B%0A
+        myHandler.post(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
         Text
@@ -4443,24 +4443,32 @@
 .url_link);%0A
+        
         Move
@@ -4521,32 +4521,40 @@
 ance();%0A        
+     
+   
 linkURL.setMovem
@@ -4569,24 +4569,32 @@
 d(method);%0A%0A
+        
         link
@@ -4617,32 +4617,40 @@
 ink());%0A        
+ 
+       
 linkURL.setVisib
@@ -4679,16 +4679,24 @@
         
+    
+    
 EditText
@@ -4776,16 +4776,24 @@
 _disp);%0A
+        
         
@@ -4814,32 +4814,40 @@
 Text(userName);%0A
+        
         userName
@@ -4878,16 +4878,42 @@
 SIBLE);%0A
+            %7D%0A        %7D);%0A
     %7D%0A%0A 
@@ -5474,15 +5474,47 @@
 =%5C%22%22
- + lUrl
+%0A                + lUrl%0A               
  + %22

KCSDK-295 simplify the code.