package com.saki.ui;
import com.saki.plugin.dictionary.DicFactroy;
import com.saki.plugin.dictionary.DicHandler;
import com.saki.sqv9.R;

import com.saki.tools.FileUtil;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class DictionaryInputActivity extends Activity implements
        OnClickListener {

    class MyHandler extends DicHandler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                info.append(msg.obj.toString() + "\n");
            } else if (msg.what == 1) {
                info.append("执行完成\n");
            } else if (msg.what == 2) {
                title.setText(path);
            }
        }
    }

    private RadioButton add;
    private RadioButton replace;
    private Button run;
    private TextView title;
    private Button select;
    private TextView info;
    private String path;
    private RadioButton old;
    private RadioButton now;
    private MyHandler h;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case FileUtil.FILE_SELECT_CODE:
                if (resultCode == RESULT_OK) {
                    // Get the Uri of the selected file
                    Uri uri = data.getData();
                    this.path = FileUtil.getPath(this, uri);
                    h.sendEmptyMessage(2);
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View v) {
        if (v == add) {
            replace.setChecked(!add.isChecked());
            old.setEnabled(!replace.isChecked());
            now.setEnabled(!replace.isChecked());
        } else if (v == replace) {
            add.setChecked(!replace.isChecked());
            old.setEnabled(add.isChecked());
            now.setEnabled(add.isChecked());
        } else if (v == old) {
            now.setChecked(!old.isChecked());
        } else if (v == now) {
            old.setChecked(!now.isChecked());
        } else if (v == select) {
            FileUtil.showFileChooser(this,FileUtil.FILE_SELECT_CODE);
        } else if (v == run) {
            if (path == null) {
                Toast.makeText(this, "请先选择一个文件!", Toast.LENGTH_SHORT).show();
                return;
            }
            String info = "您目前选择的模式为替换模式,该模式会清空原有dic的内容,确认操作吗？";
            if (add.isChecked()) {
                info = "您目前选择的模式为合并模式,该模式不会清除旧dic的部分内容，确认操作吗？";
            }
            Builder b = new Builder(this);
            b.setTitle("警告!!");
            b.setMessage(info);
            b.setPositiveButton("取消", null);
            b.setNegativeButton("继续", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (add.isChecked())
                        DicFactroy.importDic(DictionaryInputActivity.this, h,
                                path, !old.isChecked());
                    else
                        DicFactroy.importDic(DictionaryInputActivity.this, h,
                                path);
                }
            });
            b.show();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay_dictionary_input);
        h = new MyHandler();
        add = (RadioButton) findViewById(R.id.add);
        replace = (RadioButton) findViewById(R.id.replace);
        old = (RadioButton) findViewById(R.id.old);
        now = (RadioButton) findViewById(R.id.now);
        run = (Button) findViewById(R.id.run);
        select = (Button) findViewById(R.id.select);
        title = (TextView) findViewById(R.id.title);
        info = (TextView) findViewById(R.id.info);
        add.setOnClickListener(this);
        replace.setOnClickListener(this);
        run.setOnClickListener(this);
        select.setOnClickListener(this);
        old.setOnClickListener(this);
        now.setOnClickListener(this);
        info.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

}
