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.NoteListActivity;

public class syncActivity<c> extends Activity {

    public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + Notes.AUTHORITY + "/note");

    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("上传成功！");
                                NoteListActivity.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")){
                                JSONObject JSONInfo = new JSONObject(info);
                                NotesProvider.syncTABLE(JSONInfo);
                                showMessage("下载成功！");
                                NoteListActivity.finish();
                                Intent intent = new Intent(syncActivity.this, NotesListActivity.class);
                                startActivity(intent);
                                syncActivity.this.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();//暂时提示
            }
        });
    }
}
