package epson.maintain.activity;

import android.app.Dialog;


import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;



import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import com.epson.iprint.shared.SharedParamStatus;
import com.epson.mobilephone.common.escpr.MediaInfo;
import com.epson.mobilephone.common.maintain2.MaintainPrinter2;
import com.epson.mobilephone.common.maintain2.MaintainPrinterInfo2;

import epson.common.Constants;
import epson.common.DialogProgress;
import epson.common.DialogProgressViewModel;
import epson.common.Utils;
import epson.print.R;
import epson.print.CommonDefine;
import epson.print.MyPrinter;
import epson.print.Util.EPLog;

import java.util.ArrayList;
import java.util.Deque;

public class GetPrinterReplyData extends AppCompatActivity implements MediaInfo, DialogProgress.DialogButtonClick {
    private static final int CLEANING_ERROR = 10;
    private static final int DELAY_TIME_MAINTAIN = 10;
    private static final String DIALOG_PROGRESS = "dialog_progress";
    private static final int END_REPLYDATA = 99;
    private static final int EPS_COMM_BID = 2;
    private static final int EPS_LANG_ESCPR = 1;
    private static final int EPS_MNT_CLEANING = 2;
    private static final int EPS_MNT_NOZZLE = 3;
    private static final int EPS_PRNERR_CEMPTY = 103;
    private static final int EPS_PRNERR_CFAIL = 104;
    private static final int EPS_PRNERR_DISABEL_CLEANING = 108;
    private static final int EPS_PRNERR_INKOUT = 6;
    private static final int GET_PRINTER_NAME = 8;
    private static final int HANDLE_ERROR = 4;
    public static final int ITEM_HEIGHT = 40;
    private static final int MAINTAIN_OK = 1;
    private static final int MAINTAIN_START = 0;
    private static final int NO_PRINTER_NAME = 9;
    public static final String PREFS_NAME = "PrintSetting";
    private static final int PROBE_ERROR = 7;
    private static final String PROBE_RESULT = "PROBE_ERROR";
    private static final int START_LOADCONFIG = 6;
    private static final String TAG = "MAINTAIN";
    private static final int UPDATE_INK = 3;
    private static final int UPDATE_SELECTED_PRINTER = 5;
    private static final int UPDATE_STT = 2;
    public static MaintainPrinter2 mPrinter;
    private Dialog customPro;
    private SharedParamStatus extParam;
    boolean isGotError = false;
    boolean isResearchScanner = false;
    private Thread mBackGround;
    private Thread mDoMaintainThread;
    boolean mHavePrinter = false;
    volatile boolean mIsCancelProbe = false;
    boolean mIsStillUpdate = true;
    boolean mIsStop = false;
    private Thread mLoadConfigThread;
    private DialogProgressViewModel mModelDialog;
    private String mPrinterId;
    private String mPrinterIp;
    private int mPrinterLocation;
    private String mPrinterName;
    private int[] mPrinterStatus;
    boolean mStartSearch = false;
    Handler mUiHandler = new Handler() {
        public void handleMessage(Message message) {
            int i = message.what;
            if (i != 99) {
                switch (i) {
                    case 0:
                        GetPrinterReplyData.this.showProDia();
                        return;
                    case 1:
                        EPLog.i("Maintain", "********Maintain finish********");
                        GetPrinterReplyData.this.interrupMaintainThread();
                        return;
                    case 2:
                        GetPrinterReplyData.this.updateStatus(message.getData().getInt("GET_STT_RESULT"));
                        return;
                    case 3:
                        GetPrinterReplyData.this.updateInkInfo(message.getData().getInt("GET_INK_RESULT"));
                        return;
                    case 4:
                        GetPrinterReplyData.this.cancelLoadConPro();
                        GetPrinterReplyData.this.interrupMaintainThread();
                        return;
                    case 5:
                        GetPrinterReplyData.this.updateSelectedPrinter();
                        return;
                    case 6:
                        GetPrinterReplyData.this.showLoadConPro();
                        return;
                    case 7:
                        GetPrinterReplyData.this.setResult(1);
                        GetPrinterReplyData.this.finish();
                        return;
                    case 9:
                        GetPrinterReplyData.this.cancelLoadConPro();
                        GetPrinterReplyData.this.endLoadConThread();
                        GetPrinterReplyData.this.setResult(1);
                        GetPrinterReplyData.this.finish();
                        return;
                    case 10:
                        GetPrinterReplyData.this.setupErrorMessage(10);
                        GetPrinterReplyData.this.interrupMaintainThread();
                        return;
                    default:
                        return;
                }
            } else {
                GetPrinterReplyData.this.endBackGroundThread();
                GetPrinterReplyData.this.cancelLoadConPro();
                GetPrinterReplyData.this.endLoadConThread();
                Intent intent = new Intent();
                intent.putExtra("extStatus", GetPrinterReplyData.this.extParam);
                GetPrinterReplyData.this.setResult(message.getData().getInt("END_RELPY_RESULT"), intent);
                GetPrinterReplyData.this.finish();
            }
        }
    };

    private String inkCodeToString(int i) {
        switch (i) {
            case 0:
                return "Black";
            case 1:
                return "Cyan";
            case 2:
                return "Magenta";
            case 3:
                return "Yellow";
            case 4:
                return "Light Cyan";
            case 5:
                return "Light Magenta";
            case 6:
                return "Light Yellow";
            case 7:
                return "Dark Yellow";
            case 8:
                return "Light Black";
            case 9:
                return "Red";
            case 10:
                return "Blue";
            case 12:
                return "Light Gray";
            case 14:
                return "Gloss Optimizer";
            case 17:
                return "Black";
            case 18:
                return "Orange";
            case 19:
                return "Green";
            case 20:
                return "White";
            case 21:
                return "Clean";
            default:
                return "unKnown";
        }
    }

    public void onNegativeClick(String str) {
    }

    public void onNeutralClick(String str) {
    }

    public void onPositiveClick(String str) {
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mModelDialog = (DialogProgressViewModel) ViewModelProviders.m2of((FragmentActivity) this).get(DialogProgressViewModel.class);
        mModelDialog.getDialogJob().observe(this, new Observer() {
            public final void onChanged(Object obj) {
                GetPrinterReplyData.lambda$onCreate$0(GetPrinterReplyData.this, (Deque) obj);
            }
        });
        extParam = (SharedParamStatus) getIntent().getSerializableExtra("extStatus");
        if (this.extParam == null) {
            extParam = new SharedParamStatus();
        }
        if (mPrinter == null) {
            mPrinter = MaintainPrinter2.getInstance();
        }
        EPLog.i(TAG, "onCreate");
        lockRotation();
        mPrinter.doInitDriver(this, 2);
        startLoadConThread();
    }

    public static /* synthetic */ void lambda$onCreate$0(GetPrinterReplyData getPrinterReplyData, Deque deque) {
        String[] checkQueue = getPrinterReplyData.mModelDialog.checkQueue();
        if (checkQueue != null) {
            String str = checkQueue[0];
            String str2 = checkQueue[1];
            if (str2.equals("do_show")) {
                getPrinterReplyData.showDialog(str);
            }
            if (str2.equals("do_dismiss")) {
                getPrinterReplyData.dismissDialog(str);
            }
        }
    }

    private void lockRotation() {
        switch (getResources().getConfiguration().orientation) {
            case 1:
                setRequestedOrientation(1);
                return;
            case 2:
                setRequestedOrientation(0);
                return;
            default:
                return;
        }
    }

    public void startLoadConThread() {
        mIsCancelProbe = false;
        mLoadConfigThread = new Thread(new Runnable() {
            public void run() {
                EPLog.i(GetPrinterReplyData.TAG, "new load Config thread");
                GetPrinterReplyData.mUiHandler.sendEmptyMessage(6);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                GetPrinterReplyData.this.loadConfig();
                if (GetPrinterReplyData.mPrinterId == null || GetPrinterReplyData.mPrinterName == null) {
                    GetPrinterReplyData getPrinterReplyData = GetPrinterReplyData.this;
                    getPrinterReplyData.mHavePrinter = false;
                    getPrinterReplyData.mUiHandler.sendEmptyMessage(9);
                    EPLog.i(GetPrinterReplyData.TAG, "not select printer");
                    return;
                }
                GetPrinterReplyData.mUiHandler.sendEmptyMessage(8);
                EPLog.i("startLoadConThread", "mPrinter.doProbePrinter");
                if (!GetPrinterReplyData.mIsCancelProbe) {
                    int doProbePrinter = GetPrinterReplyData.mPrinter.doProbePrinter(60, GetPrinterReplyData.mPrinterId, GetPrinterReplyData.mPrinterIp, GetPrinterReplyData.mPrinterLocation);
                    while (!GetPrinterReplyData.mIsCancelProbe && doProbePrinter != 0) {
                        try {
                            Message message = new Message();
                            message.what = 7;
                            Bundle bundle = new Bundle();
                            bundle.putInt(GetPrinterReplyData.PROBE_RESULT, doProbePrinter);
                            message.setData(bundle);
                            GetPrinterReplyData.mUiHandler.sendMessage(message);
                            Thread.sleep(4000);
                            if (!GetPrinterReplyData.mIsCancelProbe) {
                                doProbePrinter = GetPrinterReplyData.mPrinter.doProbePrinter(60, GetPrinterReplyData.mPrinterId, GetPrinterReplyData.mPrinterIp, GetPrinterReplyData.mPrinterLocation);
                            }
                        } catch (InterruptedException e2) {
                            e2.printStackTrace();
                        }
                    }
                    EPLog.i(GetPrinterReplyData.TAG, "Probe Printer result: " + doProbePrinter);
                    if (doProbePrinter == 0) {
                        GetPrinterReplyData.mPrinter.setMSearchPos(0);
                        GetPrinterReplyData getPrinterReplyData2 = GetPrinterReplyData.this;
                        getPrinterReplyData2.mHavePrinter = true;
                        getPrinterReplyData2.mUiHandler.sendEmptyMessage(5);
                    }
                }
            }
        });
        mLoadConfigThread.start();
    }

    private void setupErrorMessage(int i) {
        Utils.makeMessageBox(this, getString(R.string.EPS_PRNERR_DISABEL_CLEANING_TITLE), getString(R.string.EPS_PRNERR_DISABEL_CLEANING_MSG), getString(R.string.str_ok)).show();
    }

    public void showProDia() {
        if (this.customPro == null) {
            customPro = new CustomProDialog(this, R.style.AppTheme_Translucent, R.layout.maintain_executing_dialog);
        }
        customPro.show();
    }

    public void cancelProDia() {
        Dialog dialog = customPro;
        if (dialog != null && dialog.isShowing()) {
            customPro.cancel();
            customPro = null;
        }
    }

    public void showLoadConPro() {
        mModelDialog.doShow(DIALOG_PROGRESS);
    }

    private void showDialog(String str) {
        DialogProgress dialogProgress;
        if (((str.hashCode() == -21731516 && str.equals(DIALOG_PROGRESS)) ? (char) 0 : 65535) != 0) {
            dialogProgress = null;
        } else {
            dialogProgress = DialogProgress.newInstance(str, 0, getString(R.string.str_wait));
        }
        if (dialogProgress != null) {
            dialogProgress.setCancelable(true);
            dialogProgress.show(getSupportFragmentManager(), str);
        }
    }

    private void dismissDialog(String str) {
        DialogFragment dialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(str);
        if (dialogFragment != null) {
            dialogFragment.dismiss();
        }
    }

    public void cancelLoadConPro() {
        mModelDialog.doDismiss(DIALOG_PROGRESS);
    }

    public void endLoadConThread() {
        mIsCancelProbe = true;
        Thread thread = mLoadConfigThread;
        if (thread != null && thread.isAlive()) {
            mLoadConfigThread.interrupt();
            try {
                mLoadConfigThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mLoadConfigThread = null;
        }
    }

    public void startBackgroundThread() {
        mIsStillUpdate = true;
        mBackGround = new Thread(new Runnable() {
            public void run() {
                int doGetStatus = GetPrinterReplyData.mPrinter.doGetStatus();
                Message message = new Message();
                message.what = 2;
                Bundle bundle = new Bundle();
                bundle.putInt("GET_STT_RESULT", doGetStatus);
                message.setData(bundle);
                GetPrinterReplyData.mUiHandler.sendMessage(message);
                int doGetInkInfo = GetPrinterReplyData.mPrinter.doGetInkInfo();
                Message message2 = new Message();
                message2.what = 3;
                Bundle bundle2 = new Bundle();
                bundle2.putInt("GET_INK_RESULT", doGetInkInfo);
                message2.setData(bundle2);
                GetPrinterReplyData.mUiHandler.sendMessage(message2);
            }
        });
        mBackGround.setPriority(5);
        mBackGround.start();
    }

    public void endBackGroundThread() {
        mIsStillUpdate = false;
        Thread thread = mBackGround;
        if (thread != null && thread.isAlive()) {
            mBackGround.interrupt();
            try {
                mBackGround.join();
            } catch (InterruptedException unused) {
            }
            mBackGround = null;
        }
    }

    public void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        int doGetStatus = mPrinter.doGetStatus();
        Message message = new Message();
        message.what = 2;
        Bundle bundle = new Bundle();
        bundle.putInt("GET_STT_RESULT", doGetStatus);
        message.setData(bundle);
        mUiHandler.sendMessage(message);
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 1) {
            if (i2 == 1) {
                mPrinter.setMSearchPos(intent.getIntExtra(Constants.PRINTER_POS, 0));
                mPrinterName = intent.getStringExtra(Constants.PRINTER_NAME);
                mPrinterId = intent.getStringExtra(Constants.PRINTER_ID);
                mPrinterIp = intent.getStringExtra(Constants.PRINTER_IP);
                Utils.savePref(getApplicationContext(), "PrintSetting", CommonDefine.RE_SEARCH, true);
                mUiHandler.sendEmptyMessage(8);
            }
            saveSelectedPrinter();
        }
    }

    public void updateSelectedPrinter() {
        cancelLoadConPro();
        endLoadConThread();
        int doSetPrinter = mPrinter.doSetPrinter();
        if (doSetPrinter == 0) {
            startBackgroundThread();
            mIsStillUpdate = true;
            return;
        }
        mIsStillUpdate = false;
        handlerError(doSetPrinter, false);
    }

    public void updateStatus(int i) {
        if (i == 0) {
            if (isPrinterReady() == 1) {
                int doGetInkInfo = mPrinter.doGetInkInfo();
                Message message = new Message();
                message.what = 3;
                Bundle bundle = new Bundle();
                bundle.putInt("GET_INK_RESULT", doGetInkInfo);
                message.setData(bundle);
                mUiHandler.sendMessage(message);
            }
            int[] iArr = mPrinterStatus;
            if (iArr[0] != 4) {
                return;
            }
            if (iArr[1] == 6 || iArr[1] == 103 || iArr[1] == 104) {
                int doGetInkInfo2 = mPrinter.doGetInkInfo();
                Message message2 = new Message();
                message2.what = 3;
                Bundle bundle2 = new Bundle();
                bundle2.putInt("GET_INK_RESULT", doGetInkInfo2);
                message2.setData(bundle2);
                mUiHandler.sendMessage(message2);
                return;
            }
            return;
        }
        handlerError(i, false);
    }

    public void updateInkInfo(int i) {
        int i2;
        MaintainPrinterInfo2 mPrinterInfor = mPrinter.getMPrinterInfor();
        if (i == 0) {
            int inkNum = mPrinterInfor.getInkNum();
            ArrayList arrayList = new ArrayList();
            ArrayList arrayList2 = new ArrayList();
            ArrayList arrayList3 = new ArrayList();
            i2 = -1;
            extParam.setPrinter_name(mPrinterName);
            extParam.setPrinter_status(mPrinterStatus[0]);
            for (int i3 = 0; i3 < inkNum; i3++) {
                arrayList.add(inkCodeToString(mPrinterInfor.getInkCode(i3)));
                arrayList2.add(Integer.valueOf(mPrinterInfor.getInkRemainingAmount(i3)));
                arrayList3.add(Integer.valueOf(mPrinterInfor.getInkStatus(i3)));
            }
            extParam.setArrayOutInkName(arrayList);
            extParam.setArrayOutInkRemain(arrayList2);
            extParam.setEscprlibInkStatus(arrayList3);
        } else {
            i2 = 1;
        }
        Message message = new Message();
        message.what = 99;
        Bundle bundle = new Bundle();
        bundle.putInt("END_RELPY_RESULT", i2);
        message.setData(bundle);
        mUiHandler.sendMessage(message);
    }

    public void handlerError(int i, boolean z) {
        cancelLoadConPro();
    }

    public int isPrinterReady() {
        mPrinterStatus = mPrinter.getMPrinterInfor().getMStatus();
        int[] iArr = mPrinterStatus;
        if (iArr[0] == 2 || iArr[0] == 1 || iArr[0] == 3) {
            return 0;
        }
        if (iArr[0] == 4) {
            return -1;
        }
        return 1;
    }

    public void interrupMaintainThread() {
        Thread thread = mDoMaintainThread;
        if (thread != null && thread.isAlive()) {
            mDoMaintainThread.interrupt();
            mDoMaintainThread = null;
        }
    }

    protected void onDestroy() {
        stopAllThread();
        super.onDestroy();
        EPLog.i(TAG, "onDestroy");
    }

    protected void onPause() {
        super.onPause();
    }

    protected void OLDonPause() {
        mIsStop = true;
        mIsCancelProbe = true;
        cancelLoadConPro();
        mPrinter.doCancelFindPrinter();
        endLoadConThread();
        interrupMaintainThread();
        endBackGroundThread();
        super.onPause();
    }

    protected void onStop() {
        super.onStop();
    }

    private void loadConfig() {
        MyPrinter curPrinter = MyPrinter.getCurPrinter(this);
        mPrinterId = curPrinter.getPrinterId();
        mPrinterName = curPrinter.getName();
        mPrinterIp = curPrinter.getIp();
        mPrinterLocation = curPrinter.getLocation();
    }

    private void saveSelectedPrinter() {
        Utils.savePref(getApplicationContext(), "PrintSetting", Constants.PRINTER_NAME, mPrinterName);
        Utils.savePref(getApplicationContext(), "PrintSetting", Constants.PRINTER_IP, mPrinterIp);
        Utils.savePref(getApplicationContext(), "PrintSetting", Constants.PRINTER_ID, mPrinterId);
    }

    public void onCancelDialog(String str) {
        if (((str.hashCode() == -21731516 && str.equals(DIALOG_PROGRESS)) ? (char) 0 : 65535) == 0) {
            stopAllThread();
            finish();
        }
    }

    private class CustomProDialog extends Dialog {
        private int mLayoutId;

        public CustomProDialog(Context context, int i, int i2) {
            super(context, i);
            mLayoutId = i2;
            setCancelable(false);
        }

        protected void onCreate(Bundle bundle) {
            super.onCreate(bundle);
            requestWindowFeature(1);
            View inflate = getLayoutInflater().inflate(mLayoutId, (ViewGroup) null);
            if (mLayoutId == R.layout.maintain_executing_dialog) {
                ((Button) inflate.findViewById(R.id.close_btn)).setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        CustomProDialog.this.cancel();
                        GetPrinterReplyData.this.interrupMaintainThread();
                    }
                });
            }
            setContentView(inflate);
        }
    }

    private void stopAllThread() {
        if (!mIsCancelProbe) {
            mIsStop = true;
            mIsCancelProbe = true;
            MaintainPrinter2 maintainPrinter2 = mPrinter;
            if (maintainPrinter2 != null) {
                maintainPrinter2.doCancelFindPrinter();
            }
            cancelLoadConPro();
            endLoadConThread();
            interrupMaintainThread();
            endBackGroundThread();
        }
    }
}
