package com.epson.memcardacc;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.os.AsyncTask;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.epson.iprint.prtlogger.Analytics;
import com.epson.iprint.prtlogger.CommonLog;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;
import epson.common.Utils;

import epson.print.CommonDefine;
import epson.print.Util.EPLog;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;

public class MemcardReadProgress extends Activity {
    public static final String KEY_IMAGE_LIST = "imageList";
    public static final String KEY_WRITE_FOLDER_NAME = "target_folder_name";
    public static final String LOG_TAG = "MemcardReadProgress";
    public static final String MEMCARD_STORAGE_TYPE = "memcard_storage_type";
    public static final int RESULT_COMPLETE = -2;
    public static final int RESULT_ERROR = 3;
    boolean discconectOnThreadFinish = false;
    private Button mCancelButton;
    private CopyTask mCopyTask;
    ProgressBar mEachProgress;
    private ArrayList<String> mImageList;
    private int mMemcardStorageType;
    private String mTargetFolderName;
    TextView mTextView;
    ProgressBar mTotalProgress;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        Utils.setFInishOnTOuchOutside(this, false);
        setContentView(R.layout.memcard_write_progress);
        Intent intent = getIntent();
        mImageList = intent.getStringArrayListExtra("imageList");
        mTargetFolderName = intent.getStringExtra("target_folder_name");
        mMemcardStorageType = intent.getIntExtra("memcard_storage_type", 1);
        mTotalProgress = (ProgressBar) findViewById(R.id.progress_percent);
        mTotalProgress.setVisibility(0);
        mEachProgress = (ProgressBar) findViewById(R.id.progress_copies);
        mEachProgress.setVisibility(8);
        mTextView = (TextView) findViewById(R.id.percent);
        ((TextView) findViewById(R.id.copyingMessageText)).setText(getResources().getString(R.string.memcard_file_reading_message));
        Intent intent2 = getIntent();
        mImageList = intent2.getStringArrayListExtra("imageList");
        mTargetFolderName = intent2.getStringExtra("target_folder_name");
        mCancelButton = (Button) findViewById(R.id.cancel_button);
        mCancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                MemcardReadProgress.this.actionCancel();
            }
        });
        mCopyTask = new CopyTask();
        mCopyTask.execute(new Void[0]);
    }

    protected void onStop() {
        EPLog.m305d(LOG_TAG, "onStop()");
        actionCancel();
        discconectOnThreadFinish = true;
        super.onStop();
    }

    public void actionCancel() {
        mCancelButton.setEnabled(false);
        mCopyTask.taskCancel();
    }

    public void setProgressText(int i, int i2) {
        mTextView.setText(String.format("%d/%d", new Object[]{Integer.valueOf(i), Integer.valueOf(i2)}));
    }

    public void setProgress(int i, int i2) {
        setProgressText(i, i2);
        mTotalProgress.setMax(i2);
        mTotalProgress.setProgress(i);
    }

    public void finishWithState(int i, @Nullable CommonLog commonLog) {
        if (this.discconectOnThreadFinish) {
            WiFiDirectManager.disconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, MemcardUtil.getPrinterIpAddress(this));
        }
        if (commonLog != null && commonLog.numberOfSheet > 0) {
            Analytics.sendCommonLog(this, commonLog);
        }
        setResult(i);
        finish();
    }

    class CopyTask extends AsyncTask<Void, Integer, Integer> {
        CifsAccess cifsAccess = CifsAccess.getInstance();
        private boolean mCanceling;
        private CommonLog mCommonLog = new CommonLog();

        CopyTask() {
            CommonLog commonLog = mCommonLog;
            commonLog.numberOfSheet = 0;
            commonLog.action = 8194;
            commonLog.printerName = Analytics.getDefaultPrinterName(MemcardReadProgress.this.getApplicationContext());
        }

        protected void onCancelled() {
            MemcardUtil.clearKeepScreenOn(MemcardReadProgress.this.getWindow());
            EPLog.m313i(MemcardReadProgress.LOG_TAG, "canceld in AsyncTask");
            MemcardReadProgress.this.finishWithState(0, mCommonLog);
        }

        protected void onPostExecute(Integer num) {
            MemcardUtil.clearKeepScreenOn(MemcardReadProgress.this.getWindow());
            MemcardReadProgress.this.finishWithState(num.intValue(), mCommonLog);
        }

        protected void onPreExecute() {
            mCanceling = false;
            MemcardUtil.keepScreenOn(MemcardReadProgress.this.getWindow());
            MemcardReadProgress memcardReadProgress = MemcardReadProgress.this;
            memcardReadProgress.setProgress(0, memcardReadProgress.mImageList.size());
        }

        protected void onProgressUpdate(Integer... numArr) {
            MemcardReadProgress.this.setProgress(numArr[0].intValue(), MemcardReadProgress.mImageList.size());
        }

        protected Integer doInBackground(Void... voidArr) {
            try {
                if (this.cifsAccess.initDefault(MemcardReadProgress.this, 1) == 0) {
                    return 3;
                }
                if (this.cifsAccess.connectDefaultStorageWidthDefaultAthInfo(MemcardReadProgress.mMemcardStorageType) == 0) {
                    cifsAccess.free();
                    return 3;
                }
                int copyFile = copyFile();
                cifsAccess.disconnectStorage();
                mCommonLog.setConnectionType(MemcardReadProgress.this.getApplicationContext());
                Integer valueOf = Integer.valueOf(copyFile);
                cifsAccess.free();
                return valueOf;
            } finally {
                cifsAccess.free();
            }
        }

        private int copyFile() {
            Iterator it = MemcardReadProgress.mImageList.iterator();
            int i = 0;
            while (it.hasNext()) {
                if (isCancelled()) {
                    return 0;
                }
                publishProgress(new Integer[]{Integer.valueOf(i)});
                String str = (String) it.next();
                String format = String.format(Locale.US, "%s/IMG_%04d.JPG", new Object[]{MemcardReadProgress.mTargetFolderName, Integer.valueOf(i)});
                i++;
                if (new File(format).exists()) {
                    EPLog.m313i(MemcardReadProgress.LOG_TAG, "local file exists. skip");
                } else {
                    EPLog.m313i(MemcardReadProgress.LOG_TAG, "copy " + str + " => " + format);
                    int readFromPrinterMemcard = cifsAccess.readFromPrinterMemcard(str, format);
                    if (readFromPrinterMemcard != 0) {
                        EPLog.m318w(MemcardReadProgress.LOG_TAG, "cifsAccess.writeFromPrinterMemcard() return <" + readFromPrinterMemcard + ">");
                        new File(format).delete();
                        if (readFromPrinterMemcard == -2) {
                            return 0;
                        }
                        return 3;
                    }
                    String replace = format.replace(MemcardConfig.BASE_DIRECTORY, CommonDefine.DEFAULT_DIR);
                    if (new File(replace).exists()) {
                        EPLog.m313i(MemcardReadProgress.LOG_TAG, "MediaScannerConnection.scanFile path = " + replace);
                        MediaScannerConnection.scanFile(MemcardReadProgress.this, new String[]{replace}, (String[]) null, (MediaScannerConnection.OnScanCompletedListener) null);
                    } else {
                        EPLog.m307e(MemcardReadProgress.LOG_TAG, "Failed MediaScannerConnection.scanFile path = " + replace);
                    }
                    mCommonLog.numberOfSheet = i;
                }
            }
            return -2;
        }

        public void taskCancel() {
            if (!mCanceling) {
                mCanceling = true;
                cifsAccess.cancel();
                cancel(false);
            }
        }
    }
}
