package epson.server.screens;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import com.epson.iprint.storage.StorageProcessActivity;
import com.epson.iprint.storage.StorageProcessDownloadActivity;
import com.epson.iprint.storage.StorageServiceClient;
import com.epson.iprint.storage.gdrivev3.ListAndDownloadActivity;
import com.epson.iprint.storage.onedrive.OneDriveStorageProcessUploadActivity;

import epson.common.ExternalFileUtils;
import epson.print.ActivityIACommon;
import epson.print.ActivityRequestPermissions;
import epson.print.R;
import epson.print.CommonDefine;
import epson.print.SimpleMessageDialogFragment;
import epson.print.Util.EPLog;
import epson.print.Util.Utils;
import epson.print.screen.WorkingDialog;
import epson.server.utils.Define;
import epson.server.utils.MyUtility;

import java.util.ArrayList;

public class StorageServer extends ActivityIACommon implements CommonDefine {
    public static final String FOR_PRINT_LOGGER_CALL_FROM = "forLoggerCallFrom";
    public static final int FOR_PRINT_LOGGER_CALL_FROM_NO_INFO = 0;
    public static final int FOR_PRINT_LOGGER_CALL_FROM_SCAN = 1;
    private static final int REQEST_RUNTIMEPERMMISSION = 1;
    int mNumOfFiles;
    boolean mScanSaveAction = false;
    private ArrayList<String> mScanedFilePath = new ArrayList<>();

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.choose_storage_server);
        setActionBar(R.string.network_storage_title, true);
        new AsyncTask<Void, Void, Boolean>() {
            WorkingDialog workingDialog = new WorkingDialog(StorageServer.this);

            protected void onPreExecute() {
                super.onPreExecute();
                workingDialog.show();
            }

            protected Boolean doInBackground(Void... voidArr) {
                if (!Utils.getInstance().isNetworkAvailable(StorageServer.this)) {
                    return false;
                }
                if (new MyUtility().doNet(StorageServer.this) == "") {
                    return false;
                }
                return true;
            }

            protected void onPostExecute(Boolean bool) {
                super.onPostExecute(bool);
                workingDialog.dismiss();
                StorageServer.this.initStorageItem(bool.booleanValue());
            }
        }.execute(new Void[0]);
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 1 && i2 == -1) {
            saveLocalMemory();
        }
    }

    private void initStorageItem(boolean z) {
        if (getIntent().getIntExtra(FOR_PRINT_LOGGER_CALL_FROM, 0) == 1) {
            mScanSaveAction = true;
        } else {
            mScanSaveAction = false;
        }
        final int intExtra = getIntent().getIntExtra(Define.PICK_SERVER_FOR, 1);
        if (!z) {
            findViewById(R.id.evernote).setEnabled(false);
            findViewById(R.id.google_doc).setEnabled(false);
            findViewById(R.id.drop_box).setEnabled(false);
            findViewById(R.id.box_net).setEnabled(false);
            findViewById(R.id.onedrive).setEnabled(false);
            findViewById(R.id.evernote_next).setVisibility(View.GONE);
            findViewById(R.id.google_doc_next).setVisibility(View.GONE);
            findViewById(R.id.drop_box_next).setVisibility(View.GONE);
            findViewById(R.id.box_net_next).setVisibility(View.GONE);
            findViewById(R.id.onedrive_next).setVisibility(View.GONE);
            new AlertDialog.Builder(this).setCancelable(false).setMessage(getString(R.string.str_no_network)).setPositiveButton(getString(R.string.str_ok), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            }).show();
        }
        if (intExtra == 2) {
            mScanedFilePath = getIntent().getStringArrayListExtra(Define.SAVING_FILE_PATH);
            findViewById(R.id.save_to_local_section).setVisibility(View.VISIBLE);
            setTitle(R.string.save_to_title_name);
        }
        findViewById(R.id.evernote).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                boolean z = StorageServer.mScanSaveAction;
                switch (intExtra) {
                    case 1:
                        StorageServer.this.startActivity(StorageProcessDownloadActivity.getStartIntent(StorageServer.this.getApplicationContext(), StorageServiceClient.STORAGE_EVERNOTE));
                        return;
                    case 2:
                        Intent processIntent = StorageProcessActivity.getProcessIntent(StorageServer.this.getBaseContext(), StorageServiceClient.STORAGE_EVERNOTE, StorageProcessActivity.ProcessType.UPLOAD);
                        processIntent.putStringArrayListExtra(StorageProcessActivity.EXTRA_UPLOADFILE_LIST, StorageServer.mScanedFilePath);
                        StorageServer.this.startActivity(processIntent);
                        return;
                    default:
                        return;
                }
            }
        });
        findViewById(R.id.google_doc).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                switch (intExtra) {
                    case 1:
                        StorageServer.this.startActivity(new Intent(StorageServer.this.getApplicationContext(), ListAndDownloadActivity.class));
                        return;
                    case 2:
                        Intent processIntent = StorageProcessActivity.getProcessIntent(StorageServer.this.getBaseContext(), StorageServiceClient.STORAGE_GOOGLEDRIVE, StorageProcessActivity.ProcessType.UPLOAD);
                        processIntent.putStringArrayListExtra(StorageProcessActivity.EXTRA_UPLOADFILE_LIST, StorageServer.mScanedFilePath);
                        StorageServer.this.startActivity(processIntent);
                        return;
                    default:
                        return;
                }
            }
        });
        findViewById(R.id.drop_box).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                boolean z = StorageServer.mScanSaveAction;
                switch (intExtra) {
                    case 1:
                        StorageServer.this.startActivity(StorageProcessDownloadActivity.getStartIntent(StorageServer.this.getApplicationContext(), StorageServiceClient.STORAGE_DROPBOX));
                        return;
                    case 2:
                        Intent processIntent = StorageProcessActivity.getProcessIntent(StorageServer.this.getBaseContext(), StorageServiceClient.STORAGE_DROPBOX, StorageProcessActivity.ProcessType.UPLOAD);
                        processIntent.putStringArrayListExtra(StorageProcessActivity.EXTRA_UPLOADFILE_LIST, StorageServer.mScanedFilePath);
                        StorageServer.this.startActivity(processIntent);
                        return;
                    default:
                        return;
                }
            }
        });
        findViewById(R.id.box_net).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT <= 20) {
                    StorageServer.this.showCannotUseDialog();
                    return;
                }
                switch (intExtra) {
                    case 1:
                        StorageServer.this.startActivity(StorageProcessDownloadActivity.getStartIntent(StorageServer.this.getApplicationContext(), StorageServiceClient.STORAGE_BOX));
                        return;
                    case 2:
                        Intent processIntent = StorageProcessActivity.getProcessIntent(StorageServer.this.getBaseContext(), StorageServiceClient.STORAGE_BOX, StorageProcessActivity.ProcessType.UPLOAD);
                        processIntent.putStringArrayListExtra(StorageProcessActivity.EXTRA_UPLOADFILE_LIST, StorageServer.mScanedFilePath);
                        StorageServer.this.startActivity(processIntent);
                        return;
                    default:
                        return;
                }
            }
        });
        findViewById(R.id.onedrive).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                switch (intExtra) {
                    case 1:
                        StorageServer.this.startActivity(StorageProcessDownloadActivity.getStartIntent(StorageServer.this.getApplicationContext(), StorageServiceClient.STORAGE_ONEDRIVE));
                        return;
                    case 2:
                        Intent processIntent = OneDriveStorageProcessUploadActivity.getProcessIntent(StorageServer.this.getBaseContext(), StorageServiceClient.STORAGE_ONEDRIVE, StorageProcessActivity.ProcessType.UPLOAD);
                        processIntent.putStringArrayListExtra(StorageProcessActivity.EXTRA_UPLOADFILE_LIST, StorageServer.mScanedFilePath);
                        StorageServer.this.startActivity(processIntent);
                        return;
                    default:
                        return;
                }
            }
        });
        findViewById(R.id.local_memory).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (ActivityRequestPermissions.isRuntimePermissionSupported()) {
                    String[] strArr = {"android.permission.WRITE_EXTERNAL_STORAGE"};
                    String[] strArr2 = {StorageServer.this.getString(R.string.permission_dialog_title), StorageServer.this.getString(R.string.permission_dialog_title)};
                    StorageServer storageServer = StorageServer.this;
                    StorageServer storageServer2 = StorageServer.this;
                    ActivityRequestPermissions.Permission permission = new ActivityRequestPermissions.Permission(strArr[0], strArr2, new String[]{ActivityRequestPermissions.DialogParameter.setMessage2((Context) storageServer, storageServer.getString(R.string.permission_dialog_message_storage)), ActivityRequestPermissions.DialogParameter.setMessage3A((Context) storageServer2, storageServer2.getString(R.string.permission_dialog_message_storage), StorageServer.this.getString(R.string.permission_function_storage))});
                    if (!ActivityRequestPermissions.checkPermission(StorageServer.this, strArr)) {
                        ActivityRequestPermissions.requestPermission((Activity) StorageServer.this, permission, 1);
                        return;
                    }
                }
                StorageServer.this.saveLocalMemory();
            }
        });
        findViewById(R.id.open_in).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                boolean z = StorageServer.mScanSaveAction;
                Intent intent = new Intent();
                intent.putStringArrayListExtra(Define.SAVING_FILE_PATH, StorageServer.mScanedFilePath);
                intent.putExtra("for", 6);
                intent.setClass(StorageServer.this.getBaseContext(), UploadScreen.class);
                StorageServer.this.startActivity(intent);
            }
        });
    }

    private void showCannotUseDialog() {
        SimpleMessageDialogFragment.newInstance(R.string.box_button_message).show(getSupportFragmentManager(), "can-not-use-dialog");
    }

    private void saveLocalMemory() {
        Intent intent = new Intent();
        intent.putStringArrayListExtra(Define.SAVING_FILE_PATH, mScanedFilePath);
        intent.putExtra("for", 7);
        intent.setClass(getBaseContext(), UploadScreen.class);
        startActivity(intent);
    }

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

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

    public void onBackPressed() {
        super.onBackPressed();
        EPLog.i("StorageServer ", "(onBackPressed)");
        ExternalFileUtils.getInstance(this).initDownloadDir();
    }
}
