package epson.scan.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;

import epson.common.Constants;
import epson.common.ExternalFileUtils;
import epson.print.ActivityIACommon;
import epson.print.CommonDefine;
import epson.print.R;
import epson.print.ScanFileNumber;
import epson.print.Util.EPLog;
import epson.print.Util.Utils;
import epson.print.fileBrower;
import epson.provider.ScannedFileProvider;
import epson.scan.lib.libHaru;
import epson.server.utils.Define;
import epson.server.utils.MyUtility;

public class MailActivity extends ActivityIACommon implements View.OnClickListener, CommonDefine {
    private RadioGroup chooseFileType;
    private Button clearBtn;
    private int count;
    private Intent emailIntent;
    private File fileAttachment;
    private Long fileSizeInByte;
    private float fileSizeInKb;
    private EditText mFileName;
    private String mFileType = Constants.PDF_TYPE;
    private Button saveBtn;
    private ArrayList<String> saveFilePathArray = new ArrayList<>();
    private float totalFileSize = 0.0f;
    private Utils utils = new Utils();

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.save_as);

        setActionBar(R.string.str_btn_mail, true);
        mFileName = (EditText) findViewById(R.id.save_as_dialog_file_name);
        mFileName.setFilters(new InputFilter[]{new InputFilter.LengthFilter(64)});
        mFileName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View view, boolean z) {
                if (!z) {
                    mFileName.extendSelection(0);
                    mFileName.setSelection(0);
                }
            }
        });
        chooseFileType = (RadioGroup) findViewById(R.id.file_type_for_save_as);
        saveBtn = (Button) findViewById(R.id.save_button);
        saveBtn.setText(R.string.str_btn_mail);
        saveBtn.setOnClickListener(this);
        clearBtn = (Button) findViewById(R.id.clear_mail_title_btn);
        clearBtn.setOnClickListener(this);
        chooseFileType.check(R.id.save_as_pdf_type);
        chooseFileType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.save_as_jpeg_type /*2131231434*/:
                        String unused = mFileType = Constants.JPEG_TYPE;
                        return;
                    case R.id.save_as_pdf_type /*2131231435*/:
                        String unused2 = mFileType = Constants.PDF_TYPE;
                        return;
                    default:
                        return;
                }
            }
        });
        saveFilePathArray = getIntent().getStringArrayListExtra(Define.SAVING_FILE_PATH);
        count = ScanFileNumber.getCount(this);
        EditText editText = mFileName;
        editText.setText(getString(R.string.epson) + utils.editNumber(count));
        for (int i = 0; i < saveFilePathArray.size(); i++) {
            fileAttachment = new File(saveFilePathArray.get(i));
            fileSizeInByte = Long.valueOf(fileAttachment.length());
            fileSizeInKb = (float) (fileSizeInByte.longValue() / PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID);
            EPLog.i("Data", " " + fileSizeInKb);
            totalFileSize = totalFileSize + fileSizeInKb;
        }
        if (totalFileSize > 500.0f) {
            ((TextView) findViewById(R.id.save_as_file_size)).setText("" + MyUtility.mathRound((totalFileSize / 1024.0f), 2) + "MB");
        } else {
            ((TextView) findViewById(R.id.save_as_file_size)).setText("" + MyUtility.mathRound(totalFileSize, 2) + "KB");
        }
        if (mFileName.getText().toString().length() > 0) {
            clearBtn.setVisibility(View.VISIBLE);
        }
        mFileName.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void afterTextChanged(Editable editable) {
                if (mFileName.getText().toString().equals("")) {
                    clearBtn.setVisibility(View.GONE);
                    saveBtn.setEnabled(false);
                    return;
                }
                clearBtn.setVisibility(View.VISIBLE);
                saveBtn.setEnabled(true);
            }
        });
    }

    public void onClick(View view) {
        String str;
        boolean z;
        int id = view.getId();
        if (id != R.id.clear_mail_title_btn) {
            if (id == R.id.save_button) {
                if (!fileBrower.isAvailableFileName(mFileName.getText().toString())) {
                    Toast.makeText(getApplicationContext(), R.string.str_error_Filename, 1).show();
                    return;
                }
                saveBtn.setEnabled(false);
                if (mFileType.equals(Constants.PDF_TYPE)) {
                    str = mFileName.getText().toString();
                    z = false;
                } else {
                    String obj = mFileName.getText().toString();
                    if (!obj.equals("EPSON" + utils.editNumber(count))) {
                        str = mFileName.getText().toString();
                        z = true;
                    } else if (saveFilePathArray.size() == 1) {
                        str = mFileName.getText().toString();
                        z = false;
                    } else {
                        str = "EPSON";
                        z = false;
                    }
                }
                try {
                    if (mFileType.equals(Constants.PDF_TYPE)) {
                        libHaru libharu = new libHaru();
                        String scannedImageDir = ExternalFileUtils.getInstance(this).getScannedImageDir();
                        String path = new File(scannedImageDir, str + CommonDefine.FileType_PDF).getPath();
                        libharu.createPDF(saveFilePathArray, path);
                        emailIntent = new Intent("android.intent.action.SEND");
                        fileAttachment = new File(path);
                        Uri uriForFile = ScannedFileProvider.getUriForFile(this, fileAttachment);
                        emailIntent = new Intent("android.intent.action.SEND");
                        emailIntent.putExtra("android.intent.extra.STREAM", uriForFile);
                    } else if (mFileType.equals(Constants.JPEG_TYPE)) {
                        ArrayList arrayList = new ArrayList();
                        emailIntent = new Intent("android.intent.action.SEND_MULTIPLE");
                        int i = count;
                        ExternalFileUtils.getInstance(this).createTempFolder(ExternalFileUtils.getInstance(this).getScannedImageDir());
                        if (saveFilePathArray.size() == 1) {
                            fileAttachment = MyUtility.createTempFile(this, saveFilePathArray.get(0), str + ".jpg");
                            arrayList.add(ScannedFileProvider.getUriForFile(this, fileAttachment));
                        } else {
                            if (z) {
                                i = 1;
                            }
                            for (int i2 = 0; i2 < saveFilePathArray.size(); i2++) {
                                fileAttachment = MyUtility.createTempFile(this, saveFilePathArray.get(i2), str + utils.editNumber(i) + ".jpg");
                                i++;
                                arrayList.add(ScannedFileProvider.getUriForFile(this, fileAttachment));
                            }
                        }
                        emailIntent.putParcelableArrayListExtra("android.intent.extra.STREAM", arrayList);
                    }
                    emailIntent.setType("message/rfc822");
                    emailIntent.putExtra("android.intent.extra.SUBJECT", getString(R.string.app_name));
                    emailIntent.putExtra("android.intent.extra.TEXT", getString(R.string.using_Epson_iPrint));
                    startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."), 1);
                    return;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                return;
            }
        }
        mFileName.setText("");
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 1 && i2 == 0) {
            setResult(-1);
            finish();
        }
        saveBtn.setEnabled(true);
    }
}
