package epson.epsonconnectregistration;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Xml;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epson.mobilephone.common.wifidirect.WiFiControl;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;

import epson.common.httpclient.IAHttpClient;
import epson.print.BuildConfig;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.lang.CharEncoding;
import org.xmlpull.v1.XmlPullParserException;

public class ECAvailableCheckTask extends ECBaseTask {
    private static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";
    private static final String HEALTHCHECK_BODY = "c=1";
    private static final String HEALTHCHECK_ENCODE = "aHR0cHM6Ly93d3cuZXBzb25jb25uZWN0LmNvbS9kOGQzYjkwNGJkYzk2YzZjMjc1NTAwYjVhZWZk\nM2U0YS92MS9zZXR1cA==\n";
    private static final String HEALTHCHECK_ENCODE2 = "aHR0cHM6Ly9zdGcxLWhyby13MDEuZXBzb25jb25uZWN0LmNvbS9kOGQzYjkwNGJkYzk2YzZjMjc1\nNTAwYjVhZWZkM2U0YS92MS9zZXR1cA==\n";
    private static final String HEALTHCHECK_OK = "Open";
    private static final String TAG = "ECAvailableCheckTask";
    private boolean mIsBLEContent;

    public ECAvailableCheckTask(boolean z) {
        mIsBLEContent = z;
    }

    protected ECStatus doInBackground(Void... voidArr) {
        String str;
        IAHttpClient iAHttpClient = new IAHttpClient();
        Context checkParam = checkParam();
        if (checkParam == null) {
            return ECStatus.ERROR_UNEXPECTED;
        }
        String curSSID = WiFiDirectManager.getCurSSID(checkParam);
        if (curSSID == null || !curSSID.startsWith(WiFiControl.SSID_SIMPLEAP_PREFIX)) {
            try {
                if ("Beta".equals(BuildConfig.FLAVOR)) {
                    str = new String(Base64.decode(HEALTHCHECK_ENCODE2, 0), CharEncoding.UTF_8);
                } else {
                    str = new String(Base64.decode(HEALTHCHECK_ENCODE, 0), CharEncoding.UTF_8);
                }
                IAHttpClient.HttpPost httpPost = new IAHttpClient.HttpPost(str);
                httpPost.setContentType("application/x-www-form-urlencoded");
                httpPost.setEntity(HEALTHCHECK_BODY.getBytes());
                httpPost.setContentLength(Integer.valueOf(HEALTHCHECK_BODY.getBytes().length));
                try {
                    IAHttpClient.HttpResponse execute = iAHttpClient.execute(httpPost);
                    if (execute.getResponseCode() != 200) {
                        throw new IOException("health_check ResponseCode =  " + execute.getResponseCode());
                    } else if (!HEALTHCHECK_OK.equals(execute.getEntity().toString())) {
                        EpLog.m83w(TAG, "EC Service Down");
                        return ECStatus.ERROR_CANNOT_ENABLE;
                    } else {
                        Uri endpoint = getEndpoint();
                        IAHttpClient.HttpPost httpPost2 = new IAHttpClient.HttpPost(endpoint.toString());
                        SoapWEBSETUPDataInfo soapWEBSETUPDataInfo = new SoapWEBSETUPDataInfo();
                        SoapAUTHENTICATIONDataInfo soapAUTHENTICATIONDataInfo = new SoapAUTHENTICATIONDataInfo();
                        try {
                            String createGetRequest = SoapRequestFactory.createGetRequest(checkParam, endpoint);
                            httpPost2.setContentType("application/soap+xml");
                            byte[] bytes = createGetRequest.getBytes(Xml.Encoding.UTF_8.name());
                            httpPost2.setEntity(bytes);
                            httpPost2.setContentLength(Integer.valueOf(bytes.length));
                            IAHttpClient.HttpResponse execute2 = iAHttpClient.execute(httpPost2);
                            if (execute2.getResponseCode() == 200) {
                                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(execute2.getEntity().toByteArray());
                                soapWEBSETUPDataInfo.parseXml(byteArrayInputStream);
                                ECStatus checkECPrinterStatus = soapWEBSETUPDataInfo.checkECPrinterStatus();
                                if (checkECPrinterStatus != ECStatus.ERROR_NONE) {
                                    return checkECPrinterStatus;
                                }
                                soapAUTHENTICATIONDataInfo.parseXml(byteArrayInputStream);
                                if (soapAUTHENTICATIONDataInfo.isAdminMode()) {
                                    return ECStatus.ERROR_PRINTER_ADMIN_MODE;
                                }
                                return ECStatus.ERROR_NONE;
                            } else if (execute2.getResponseCode() == 500) {
                                return ECStatus.ERROR_PRINTER_NOT_SUPPORT_EC;
                            } else {
                                throw new IOException("soapRequest ResponseCode =  " + execute2.getResponseCode());
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            return ECStatus.ERROR_COMMUNICATION;
                        } catch (XmlPullParserException e2) {
                            e2.printStackTrace();
                            return ECStatus.ERROR_UNEXPECTED;
                        }
                    }
                } catch (IOException e3) {
                    e3.printStackTrace();
                    return ECStatus.ERROR_COMMUNICATION;
                }
            } catch (UnsupportedEncodingException e4) {
                e4.printStackTrace();
                return ECStatus.ERROR_UNEXPECTED;
            }
        } else {
            EpLog.m83w(TAG, "Connected SimpleAP !!");
            return ECStatus.ERROR_CANNOT_ENABLE;
        }
    }

    protected void onPostExecute(ECStatus eCStatus) {
        super.onPostExecute(eCStatus);
        Activity activity = (Activity) activityWeakReference.get();
        if (activity == null) {
            EpLog.m83w(TAG, "activity is null. Maybe activity died");
        } else if (!mIsBLEContent) {
            if (C19985.$SwitchMap$epson$epsonconnectregistration$ECStatus[eCStatus.ordinal()] != 1) {
                createECDialog(eCStatus);
                return;
            }
            openWebPage(activity, Uri.parse("https://www.epsonconnect.com/user/"));
            activity.finish();
        } else if (C19985.$SwitchMap$epson$epsonconnectregistration$ECStatus[eCStatus.ordinal()] != 2) {
            activity.finish();
        } else {
            createECDialog(eCStatus);
        }
    }

    private void startECConfigure(Activity activity, Uri uri) {
        ECConfigurationTask eCConfigurationTask = new ECConfigurationTask();
        eCConfigurationTask.setContext(activity);
        eCConfigurationTask.setRootUri(uri);
        eCConfigurationTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
    }

    private void createECDialog(ECStatus eCStatus) {
        final Activity activity = (Activity) activityWeakReference.get();
        if (activity != null) {
            View inflate = activity.getLayoutInflater().inflate(R.layout.dialog_epsonconnect, (ViewGroup) null);
            TextView textView = (TextView) inflate.findViewById(R.id.epsonconnect_title_tv);
            TextView textView2 = (TextView) inflate.findViewById(R.id.epsonconnect_msg_tv);
            if (mIsBLEContent) {
                textView.setText(R.string.ec_ble_registration_dialog_title);
            } else {
                textView.setText(R.string.ec_registration_dialog_title);
            }
            if (eCStatus != ECStatus.ERROR_NONE) {
                inflate.findViewById(R.id.epsonconnect_title_tv).setVisibility(View.GONE);
                inflate.findViewById(R.id.epsonconnect_sub_msg_tv).setVisibility(View.GONE);
                switch (eCStatus) {
                    case ERROR_PRINTER_NOT_SUPPORT_EC:
                        textView2.setText(R.string.ec_registration_dialog_not_support_ec);
                        break;
                    case ERROR_PRINTER_ADMIN_MODE:
                        textView2.setText(R.string.ec_registration_dialog_admin_mode);
                        break;
                    default:
                        textView2.setText(R.string.ec_registration_dialog_communication_error);
                        break;
                }
            }
            final C19941 r2 = new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface dialogInterface) {
                    dialogInterface.dismiss();
                    activity.finish();
                }
            };
            AlertDialog.Builder view = new AlertDialog.Builder(activity).setView(inflate);
            if (C19985.$SwitchMap$epson$epsonconnectregistration$ECStatus[eCStatus.ordinal()] != 2) {
                view.setNegativeButton(R.string.ec_registration_button_close, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        r2.onCancel(dialogInterface);
                    }
                });
            } else {
                view.setNeutralButton(R.string.ec_registration_button_signup, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ECAvailableCheckTask eCAvailableCheckTask = ECAvailableCheckTask.this;
                        eCAvailableCheckTask.startECConfigure(activity, eCAvailableCheckTask.rootUri);
                    }
                }).setPositiveButton(R.string.ec_registration_button_skip, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        r2.onCancel(dialogInterface);
                    }
                });
            }
            AlertDialog create = view.create();
            create.setCanceledOnTouchOutside(false);
            create.setOnCancelListener(r2);
            create.setOnShowListener($$Lambda$ECAvailableCheckTask$gioJU6TFsLI9iDXVbpchuMiYbOo.INSTANCE);
            create.show();
        }
    }

    static /* synthetic */ void lambda$createECDialog$0(DialogInterface dialogInterface) {
        AlertDialog alertDialog = (AlertDialog) dialogInterface;
        alertDialog.getButton(-1).setAllCaps(false);
        alertDialog.getButton(-3).setAllCaps(false);
    }
}
