package epson.print.storage;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import epson.print.R;

public class StorageWaitingActivity extends Activity {
    static String CANCEL_REQUEST_ACTION = "StorageProgressActivity.CancelRequestAction";
    static String CHANGE_ACTION = "StorageProgressActivity.ChangeAction";
    static String DISMISS_ACTION = "StorageProgressActivity.DismissAction";
    static String EXTRA_CANCEL_ENABLED = "StorageProgressActivity.ExtraCancelEnabled";
    static String EXTRA_MESSAGE = "StorageProgressActivity.ExtraMessage";
    static String EXTRA_REQUEST_CODE = "StorageProgressActivity.ExtraRequestCode";
    static String REGISTER_ACTION = "StorageProgressActivity.RegisterAction";
    public static boolean bShowing = false;
    BroadcastReceiver mBroadcastReceiver;
    Button mCancelButton;
    TextView mMessageText;
    int mRequestCode;

    public void onBackPressed() {
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.storage_progress);
        Intent intent = getIntent();
        registerBroadcastReceiver(intent);
        setMessage(intent);
        setCancelRequest(intent);
    }

    protected void onDestroy() {
        unregisterReceiver(mBroadcastReceiver);
        super.onDestroy();
    }

    private void registerBroadcastReceiver(Intent intent) {
        mRequestCode = intent.getIntExtra(EXTRA_REQUEST_CODE, 0);
        mBroadcastReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (mRequestCode == intent.getIntExtra(EXTRA_REQUEST_CODE, 0)) {
                    if (action.equals(DISMISS_ACTION)) {
                        finish();
                    } else if (action.equals(CHANGE_ACTION)) {
                        setMessage(intent);
                    } else if (action.equals(REGISTER_ACTION)) {
                        setCancelRequest(intent);
                    }
                }
            }
        };
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(DISMISS_ACTION);
        intentFilter.addAction(CHANGE_ACTION);
        intentFilter.addAction(REGISTER_ACTION);
        registerReceiver(mBroadcastReceiver, intentFilter);
    }

    private Dialog createCancelDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(getString(R.string.str_msg_scan_cancel));
        builder.setCancelable(false);
        builder.setPositiveButton(getString(R.string.str_yes), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                mCancelButton.setEnabled(false);
                mMessageText.setText(getString(R.string.EPS_PRNST_CANCELLING_MSG));
                Intent intent = new Intent(CANCEL_REQUEST_ACTION);
                intent.putExtra(EXTRA_REQUEST_CODE, mRequestCode);
                sendBroadcast(intent);
                dialogInterface.cancel();
            }
        });
        builder.setNegativeButton(getString(R.string.str_no), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {
                mCancelButton.setEnabled(true);
                dialogInterface.cancel();
            }
        });
        return builder.create();
    }

    private void setCancelRequest(Intent intent) {
        mCancelButton = (Button) findViewById(R.id.cancelButton);
        mCancelButton.setText(getString(R.string.str_cancel));
        if (intent.getBooleanExtra(EXTRA_CANCEL_ENABLED, false)) {
            mCancelButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    createCancelDialog().show();
                }
            });
            mCancelButton.setVisibility(0);
            return;
        }
        mCancelButton.setVisibility(8);
    }

    private void setMessage(Intent intent) {
        mMessageText = (TextView) findViewById(R.id.messageText);
        String stringExtra = intent.getStringExtra(EXTRA_MESSAGE);
        if (stringExtra != null) {
            mMessageText.setText(stringExtra);
            mMessageText.setVisibility(0);
            return;
        }
        mMessageText.setVisibility(8);
    }

    protected void onResume() {
        super.onResume();
        bShowing = true;
    }

    protected void onPause() {
        super.onPause();
        bShowing = false;
    }
}
