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));
        this.mBaseDirTextView = (TextView) findViewById(R.id.baseDirText);
        this.mTargetDir = (EditText) findViewById(R.id.memcardUsername);
        this.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 = this.mBaseDirTextView;
        textView.setText(stringExtra + CommonDefine.SLASH);
        this.mTargetDir.setText(stringExtra2);
        this.mTargetDir.addTextChangedListener(new TargetDirTextWatcher());
        this.mButtonActionProgress = false;
    }

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

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

    public void cancel_button_clicked(View view) {
        if (!this.mButtonActionProgress) {
            this.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() {
        this.mOkButton.setClickable(false);
        this.mOkButton.setEnabled(false);
    }

    private void enableOkButton() {
        this.mOkButton.setEnabled(true);
        this.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();
            }
        }
    }
}
