package net.micode.notes.ui;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import net.micode.notes.data.Notes;
import net.micode.notes.data.NotesProvider;
import net.micode.notes.gtask.data.SqlData;
import net.micode.notes.R;
import net.micode.notes.mysql.ConnectRDS;

import org.json.JSONObject;
import static net.micode.notes.ui.NotesListActivity.NotesListActivity;

public class syncActivity<c> extends Activity {
    private static final String TAG = SqlData.class.getSimpleName();

    private static final int INVALID_ID = -99999;

    public static final String[] PROJECTION_DATA = new String[] {
            Notes.DataColumns.ID, Notes.DataColumns.MIME_TYPE, Notes.DataColumns.CONTENT, Notes.DataColumns.DATA1,
            Notes.DataColumns.DATA3
    };
    public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + Notes.AUTHORITY + "/note");

    public static final int SYNCUPLOAD = 3;
    /**
     * Uri to query data
     */
    public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + Notes.AUTHORITY + "/data");


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sync);

        findViewById(R.id.syncupload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)  {

                JSONObject JSnote = new JSONObject();
                JSONObject JSdata = new JSONObject();
                final JSONObject JS = new JSONObject();

                try{
                    JSdata = NotesProvider.uploaddata();
                    JSnote = NotesProvider.uploadnote();
                    JS.put("NOTE",JSnote);
                    JS.put("DATA",JSdata);
                }catch (Exception e)
                {
                    e.printStackTrace();
                }

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            if (ConnectRDS.uploadInfo(JS.toString())){
                                showMessage("上传成功！");
                                NotesListActivity.finish();
                                Intent intent = new Intent(syncActivity.this, NotesListActivity.class);
                                startActivity(intent);
                                syncActivity.this.finish();
                            } else {
                                showMessage("上传失败，请检查网络连接！");
                            }
                        } catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });


        findViewById(R.id.syncdownload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            String info = ConnectRDS.downloadInfo(LoginActivity.getUserAccount(),LoginActivity.getUserPwd());
                            if (!info.equals("failed")){
                                System.out.println(info);
                                JSONObject JSONInfo = new JSONObject(info);
                                System.out.println(JSONInfo.get("NOTE"));
                                NotesProvider.syncTABLE(JSONInfo);
                                showMessage("下载成功！");
                                NotesListActivity.finish();
                                Intent intent = new Intent(syncActivity.this, NotesListActivity.class);
                                startActivity(intent);
                                finish();
                            } else {
                                showMessage("下载失败，请检查网络连接！");
                            }
                        } catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }

    private void showMessage(final String msg) {
        if (TextUtils.isEmpty(msg))
            return;

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();//暂时提示
            }
        });
    }
}
