package com.epson.memcardacc;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import epson.print.CommonDefine;
import java.io.File;

public class DirectorySelecterActivity extends Activity {
    private static final int DIALOG_FILE_NAME_INVALID = 1;
    public static final String PARAM_BASE_DIRECTORY = "baseDirectory";
    public static final String PARAM_DEFAULT_DIR = "default";
    public static final String RESULT_DIRECTORY = "resultDirectory";
    TextView mBaseDirTextView;
    boolean mButtonActionProgress;
    Button mOkButton;
    EditText mTargetDir;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_directory_selecter);
        setTitle(getResources().getText(R.string.memcard_folder_save_to_string));
        mBaseDirTextView = (TextView) findViewById(R.id.baseDirText);
        mTargetDir = (EditText) findViewById(R.id.memcardUsername);
        mOkButton = (Button) findViewById(R.id.okButton);
        Intent intent = getIntent();
        String stringExtra = intent.getStringExtra(PARAM_BASE_DIRECTORY);
        String stringExtra2 = intent.getStringExtra(PARAM_DEFAULT_DIR);
        TextView textView = mBaseDirTextView;
        textView.setText(stringExtra + CommonDefine.SLASH);
        mTargetDir.setText(stringExtra2);
        mTargetDir.addTextChangedListener(new TargetDirTextWatcher());
        mButtonActionProgress = false;
    }

    private void localShowDialog(int i) {
        showDialog(i);
    }

    public void ok_button_clicked(View view) {
        if (!mButtonActionProgress) {
            mButtonActionProgress = true;
            Intent intent = new Intent();
            String obj = mTargetDir.getText().toString();
            if (!MemcardUtil.checkFileName(obj)) {
                localShowDialog(1);
                mButtonActionProgress = false;
                return;
            }
            File file = new File(new File(mBaseDirTextView.getText().toString()), obj);
            if (file.exists() || file.mkdirs()) {
                intent.putExtra(RESULT_DIRECTORY, obj);
                setResult(-1, intent);
                finish();
                return;
            }
            localShowDialog(1);
            mButtonActionProgress = false;
        }
    }

    public void cancel_button_clicked(View view) {
        if (!mButtonActionProgress) {
            mButtonActionProgress = true;
            setResult(0);
            finish();
        }
    }

    protected Dialog onCreateDialog(int i) {
        if (i != 1) {
            return null;
        }
        return new AlertDialog.Builder(this).setMessage(getString(R.string.str_error_Filename)).setNegativeButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        }).create();
    }

    private void disableOkButton() {
        mOkButton.setClickable(false);
        mOkButton.setEnabled(false);
    }

    private void enableOkButton() {
        mOkButton.setEnabled(true);
        mOkButton.setClickable(true);
    }

    class TargetDirTextWatcher implements TextWatcher {
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        TargetDirTextWatcher() {
        }

        public void afterTextChanged(Editable editable) {
            if (editable.length() <= 0) {
                DirectorySelecterActivity.this.disableOkButton();
            } else {
                DirectorySelecterActivity.this.enableOkButton();
            }
        }
    }
}
