package epson.print.gdconv;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.appcompat.app.AppCompatActivity;

import com.epson.iprint.storage.gdrivev3.GoogleV3UploadSignInActivity;
import com.epson.iprint.storage.gdrivev3.IprintGoogleSignIn;

import epson.print.CommonDefine;
import epson.print.GdataConvert;
import epson.print.R;

public class ConvertProgressActivity extends AppCompatActivity implements ConvertTask.TaskCallback {
    private static final int REQUEST_CODE_SIGN_IN = 10;
    private ConvertTask mConvertTask;
    private IprintGoogleSignIn mIprintGoogleSignIn;
    private String mLocalOfficePath;
    private TextView mMessageText;

    @NonNull
    public static Intent getStartIntent(Context context, String str) {
        Intent intent = new Intent(context, ConvertProgressActivity.class);
        intent.putExtra(CommonDefine.FILE_NAME, str);
        return intent;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.activity_convert_progress);
        mIprintGoogleSignIn = new IprintGoogleSignIn();
        ((Button) findViewById(R.id.close_btn)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ConvertProgressActivity.this.cancelTaskAndFinish();
            }
        });
        mMessageText = (TextView) findViewById(R.id.loading_tv);
        changeMessageText(ConvertStatus.INIT);
        if (!setLocalOfficePathFromIntent(getIntent())) {
            setResult(101);
            finish();
            return;
        }
        int checkFileSize = GdataConvert.checkFileSize(this.mLocalOfficePath);
        if (checkFileSize != 100) {
            setResult(checkFileSize);
            finish();
            return;
        }
        startActivityForResult(new Intent(this, GoogleV3UploadSignInActivity.class), 10);
    }

    /* access modifiers changed from: private */
    @UiThread
    public void cancelTaskAndFinish() {
        ConvertTask convertTask = mConvertTask;
        if (convertTask != null) {
            convertTask.cancel(true);
        }
        setResult(103);
        finish();
    }

    private boolean setLocalOfficePathFromIntent(@Nullable Intent intent) {
        if (intent == null) {
            return false;
        }
        mLocalOfficePath = intent.getStringExtra(CommonDefine.FILE_NAME);
        if (this.mLocalOfficePath != null) {
            return true;
        }
        return false;
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        if (i == 10) {
            if (i2 != -1 || !this.mIprintGoogleSignIn.isSignInValid(this, IprintGoogleSignIn.OperationType.UPLOAD)) {
                setResult(0);
                finish();
                return;
            }
            startConvertTask();
        }
    }

    private void startConvertTask() {
        mConvertTask = new ConvertTask(getApplicationContext(), this);
        mConvertTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new String[]{this.mLocalOfficePath});
    }

    public void changeMessageText(ConvertStatus convertStatus) {
        int i;
        switch (convertStatus) {
            case DOWNLOAD:
                i = R.string.download_google_docs_convert;
                break;
            case UPLOAD:
                i = R.string.upload_google_docs_convert;
                break;
            default:
                i = R.string.convert_google_docs_convert;
                break;
        }
        mMessageText.setText(i);
    }

    public void onBackPressed() {
        cancelTaskAndFinish();
    }

    public void messageChange(ConvertStatus convertStatus) {
        changeMessageText(convertStatus);
    }

    public void notifyTaskEnd(int i) {
        if (i == 100) {
            Intent intent = getIntent();
            intent.putExtra(CommonDefine.DOWNLOAD_PATH, mConvertTask.getOutFile().getAbsolutePath());
            setResult(100, intent);
        } else {
            setResult(i);
        }
        finish();
    }
}
