package epson.print.screen;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.epson.mobilephone.common.escpr.MediaInfo;
import com.epson.mobilephone.common.maintain2.MaintainPrinter2;

import epson.common.Constants;
import epson.common.Utils;
import epson.print.ActivityIACommon;
import epson.print.EPPrinterInfo;
import epson.print.EPPrinterManager;
import epson.print.R;
import epson.print.Util.EPLog;
import epson.scan.lib.escanLib;

public class ActivityIpPrinterSetting extends ActivityIACommon {
    private static final String IPADDRESS_EXP = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
    public static final int PRINTER = 0;
    private static final String PRINTER_ID = "id";
    private static final String PRINTER_NAME = "name";
    private static final String PRINTER_SERIAL_NO = "serial_no";
    public static final int SCANNER = 1;
    private static final String TAG = "ActivityIpPrinterSetting";
    static MaintainPrinter2 mPrinter = MaintainPrinter2.getInstance();
    static escanLib mScanner = new escanLib();
    private final int ADD_PRINTER = 0;
    private final int ADD_SCANNER = 8;
    Button clearPrinterIpAdressButton;
    Button clearPrinterNameButton;
    Handler mHandler = new Handler(new Handler.Callback() {
        public boolean handleMessage(Message message) {
            Bundle data = message.getData();
            int i = message.what;
            if (i == 0) {
                EPLog.w(ActivityIpPrinterSetting.TAG, "Recieve ADD_PRINTER");
                ActivityIpPrinterSetting.this.printerInfo.printerName = data.getString("name");
                ActivityIpPrinterSetting.this.printerInfo.printerID = data.getString("id");
                ActivityIpPrinterSetting.this.printerInfo.printerSerialNo = data.getString("serial_no");
                return false;
            } else if (i != 8) {
                return false;
            } else {
                EPLog.w(ActivityIpPrinterSetting.TAG, "Recieve ADD_SCANNER");
                ActivityIpPrinterSetting.this.printerInfo.scannerID = data.getString("id");
                return false;
            }
        }
    });
    EPPrinterManager manager;
    String oldItemKey = null;
    EPPrinterInfo printerInfo;
    TextView printerIpAdress;
    TextView printerName;
    ViewGroup printerNameArea;

    protected void onCreate(Bundle bundle) {
        Bundle extras;
        super.onCreate(bundle);
        setContentView((int) R.layout.ipprinter_setting_layout);
        setActionBar((int) R.string.str_ipprinter_add, true);
        printerNameArea = (ViewGroup) findViewById(R.id.printerNameArea);
        printerName = (TextView) findViewById(R.id.printer_name_edit);
        printerIpAdress = (TextView) findViewById(R.id.printer_ip_address_edit);
        clearPrinterNameButton = (Button) findViewById(R.id.clear_printer_name_btn);
        clearPrinterIpAdressButton = (Button) findViewById(R.id.clear_printer_ip_address_btn);
        manager = new EPPrinterManager(this);
        Intent intent = getIntent();
        if (!(intent == null || (extras = intent.getExtras()) == null)) {
            oldItemKey = extras.getString(Constants.PRINTER_KEY);
            String str = oldItemKey;
            if (str != null && str.length() > 0) {
                printerInfo = manager.loadIpPrinterInfo(this.oldItemKey);
            }
        }
        EPPrinterInfo ePPrinterInfo = printerInfo;
        if (ePPrinterInfo == null) {
            printerNameArea.setVisibility(8);
        } else {
            printerName.setText(ePPrinterInfo.userDefName);
            printerIpAdress.setText(this.printerInfo.printerIP);
            setTitle(R.string.str_ipprinter_edit);
        }
        clearPrinterNameButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ActivityIpPrinterSetting.this.printerName.setText("");
            }
        });
        clearPrinterIpAdressButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ActivityIpPrinterSetting.this.printerIpAdress.setText("");
            }
        });
    }

    public void onSaveButton() {
        String charSequence = printerIpAdress.getText().toString();
        if (this.printerNameArea.getVisibility() != 8 && printerName.length() <= 0) {
            new AlertDialog.Builder(this).setCancelable(false).setMessage(getString(R.string.epsonconnect_str_printer_name_error_empty)).setNegativeButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            }).show();
        } else if (charSequence.length() == 0) {
            new AlertDialog.Builder(this).setCancelable(false).setMessage(getString(R.string.str_ipprinter_ip_null)).setNegativeButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            }).show();
        } else if (!charSequence.matches(IPADDRESS_EXP)) {
            new AlertDialog.Builder(this).setCancelable(false).setMessage(getString(R.string.str_ipprinter_ip_fomaterror)).setNegativeButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            }).show();
        } else {
            new AsyncTask<String, Void, Integer>() {
                boolean bAddNewPrinter = false;
                public String mNewIpAddress;
                WorkingDialog progress;

                protected void onPreExecute() {
                    progress = new WorkingDialog(ActivityIpPrinterSetting.this);
                    progress.show();
                    ActivityIpPrinterSetting.mPrinter.getMEscpLib().setHanlder(ActivityIpPrinterSetting.this.mHandler);
                    ActivityIpPrinterSetting.mScanner.setScanHandler(ActivityIpPrinterSetting.this.mHandler);
                }

                protected Integer doInBackground(String... strArr) {
                    boolean z;
                    String str = strArr[0];
                    if (ActivityIpPrinterSetting.this.printerNameArea.getVisibility() == 8) {
                        ActivityIpPrinterSetting.this.printerInfo = new EPPrinterInfo();
                        ActivityIpPrinterSetting.this.printerInfo.printerLocation = 3;
                        ActivityIpPrinterSetting.this.printerInfo.printerID = null;
                        ActivityIpPrinterSetting.this.printerInfo.scannerID = null;
                        bAddNewPrinter = true;
                    }
                    mNewIpAddress = str;
                    int doInitDriver = ActivityIpPrinterSetting.mPrinter.doInitDriver(ActivityIpPrinterSetting.this, 192);
                    if (doInitDriver != -1050 && doInitDriver != 0) {
                        return Integer.valueOf(doInitDriver);
                    }
                    ActivityIpPrinterSetting.mPrinter.getMEscpLib().setSearchStt(true);
                    int doProbePrinter = ActivityIpPrinterSetting.mPrinter.doProbePrinter(60, ActivityIpPrinterSetting.this.printerInfo.printerID, str, 3);
                    ActivityIpPrinterSetting.mPrinter.getMEscpLib().setSearchStt(false);
                    if (doProbePrinter != 0) {
                        return Integer.valueOf(doProbePrinter);
                    }
                    int escanWrapperInitDriver = ActivityIpPrinterSetting.mScanner.escanWrapperInitDriver(ActivityIpPrinterSetting.this);
                    if (escanWrapperInitDriver == -1050) {
                        z = true;
                    } else if (escanWrapperInitDriver != 0) {
                        return Integer.valueOf(escanWrapperInitDriver);
                    } else {
                        z = false;
                    }
                    ActivityIpPrinterSetting.mScanner.setSearchStt(true);
                    int probeScannerByIp = ActivityIpPrinterSetting.mScanner.probeScannerByIp(ActivityIpPrinterSetting.this.printerInfo.scannerID, str);
                    ActivityIpPrinterSetting.mScanner.setSearchStt(false);
                    if (!z) {
                        ActivityIpPrinterSetting.mScanner.escanWrapperReleaseDriver();
                    }
                    if (probeScannerByIp == -1306) {
                        EPLog.w(ActivityIpPrinterSetting.TAG, "eScanLib return ESCAN_ERR_SCANNER_NOT_USEFUL");
                        probeScannerByIp = 0;
                    }
                    return Integer.valueOf(probeScannerByIp);
                }

                protected void onPostExecute(Integer num) {
                    Integer[] numArr;
                    if (this.progress.isShowing()) {
                        progress.dismiss();
                    }
                    if (num.intValue() != 0) {
                        if (-500001 == num.intValue()) {
                            numArr = new Integer[]{Integer.valueOf(R.string.str_ipprinter_comerror_edit), Integer.valueOf(R.string.EPS_PRNERR_COMM_TITLE2), 0};
                        } else {
                            numArr = MediaInfo.ErrorTable.getStringId(num.intValue());
                            if (numArr == null) {
                                numArr = new Integer[]{Integer.valueOf(R.string.NOT_IMPLEMENT), Integer.valueOf(R.string.NOT_IMPLEMENT_TITLE), 1};
                            }
                        }
                        ActivityIpPrinterSetting activityIpPrinterSetting = ActivityIpPrinterSetting.this;
                        Utils.makeMessageBox(activityIpPrinterSetting, activityIpPrinterSetting.getString(numArr[1].intValue()), ActivityIpPrinterSetting.this.getString(numArr[0].intValue()), ActivityIpPrinterSetting.this.getString(R.string.str_ok)).show();
                    } else if (!this.bAddNewPrinter || ActivityIpPrinterSetting.this.manager.loadIpPrinterInfo(ActivityIpPrinterSetting.this.printerInfo.printerID) == null) {
                        if (ActivityIpPrinterSetting.this.oldItemKey != null && ActivityIpPrinterSetting.this.oldItemKey.length() > 0) {
                            ActivityIpPrinterSetting.this.manager.deleteIpPrinterInfo(ActivityIpPrinterSetting.this.oldItemKey);
                        }
                        if (ActivityIpPrinterSetting.this.printerName.getText().toString().length() > 0) {
                            ActivityIpPrinterSetting.this.printerInfo.userDefName = ActivityIpPrinterSetting.this.printerName.getText().toString();
                        } else {
                            ActivityIpPrinterSetting.this.printerInfo.userDefName = ActivityIpPrinterSetting.this.printerInfo.printerName;
                        }
                        ActivityIpPrinterSetting.this.manager.saveIPPrinterInfo(ActivityIpPrinterSetting.this.printerInfo.printerID, ActivityIpPrinterSetting.this.printerInfo);
                        ActivityIpPrinterSetting.this.printerInfo.printerIP = mNewIpAddress;
                        Intent intent = new Intent();
                        intent.putExtra(Constants.PRINTER_NAME, ActivityIpPrinterSetting.this.printerInfo.printerName);
                        intent.putExtra(Constants.PRINTER_ID, ActivityIpPrinterSetting.this.printerInfo.printerID);
                        intent.putExtra(Constants.SCAN_REFS_SCANNER_ID, ActivityIpPrinterSetting.this.printerInfo.scannerID);
                        intent.putExtra(Constants.PRINTER_IP, ActivityIpPrinterSetting.this.printerInfo.printerIP);
                        intent.putExtra(Constants.PRINTER_SERIAL_NO, ActivityIpPrinterSetting.this.printerInfo.printerSerialNo);
                        ActivityIpPrinterSetting.this.setResult(-1, intent);
                        ActivityIpPrinterSetting.this.finish();
                    } else {
                        new AlertDialog.Builder(ActivityIpPrinterSetting.this).setCancelable(false).setMessage(ActivityIpPrinterSetting.this.getString(R.string.str_ipprinter_registed_already)).setNegativeButton(ActivityIpPrinterSetting.this.getString(R.string.str_ok), new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        }).show();
                    }
                }
            }.execute(new String[]{charSequence});
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.C2138menu.menu_done, menu);
        return super.onCreateOptionsMenu(menu);
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != R.id.menuSettingsDone) {
            return super.onOptionsItemSelected(menuItem);
        }
        onSaveButton();
        return true;
    }
}
