package com.epson.cameracopy.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;

import epson.print.ActivityIACommon;
import epson.print.ActivityRequestPermissions;

/* renamed from: com.epson.cameracopy.ui.FolderSelectActivity */
public class FolderSelectActivity extends ActivityIACommon implements View.OnClickListener {
    private static final int MSG_UPDATE_LIST_VIEW = 901;
    private static final int REQEST_RUNTIMEPERMMISSION = 1;
    private static WeakReference<Activity> mActivity;
    private static FolderSelectActivity mFolderSelectActivity;
    private static Handler mHandler = new Handler() {
        public void handleMessage(Message message) {
            if (FolderSelectActivity.mFolderSelectActivity != null) {
                FolderSelectActivity.mFolderSelectActivity.executeMessage(message);
            }
        }
    };
    AdapterView.OnItemClickListener listViewListener = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
            FolderSelectActivity.mFolderInformation.UpdateFolder(i);
            FolderSelectActivity.mHandler.sendEmptyMessage(FolderSelectActivity.MSG_UPDATE_LIST_VIEW);
        }
    };
    private FolderInformation mFolderInformation = null;
    private LinearLayout mFolderSelectBase = null;
    private LinearLayout mFolderSelectCurrent = null;
    private TextView mFolderSelectText = null;
    private ItemDataAdapter mItemDataAdapter = null;
    private ArrayList<ItemData> mItemDataList = null;
    private ListView mListView = null;
    private MenuControl mMenuControl = null;
    private String mRootFolder = null;

    public void onActionbarMenuNewClicked(View view) {
    }

    public void onClick(View view) {
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_folder_select);
        setActionBar(R.string.folder_selector_title, true);
        mActivity = new WeakReference<>(this);
        mFolderSelectActivity = (FolderSelectActivity) mActivity.get();
        mFolderSelectBase = (LinearLayout) findViewById(R.id.folder_select_base);
        mFolderSelectCurrent = (LinearLayout) findViewById(R.id.folder_select_current);
        mFolderSelectText = (TextView) findViewById(R.id.folder_select_text);
        mMenuControl = new MenuControl(this, 1);
        mRootFolder = Environment.getExternalStorageDirectory().getAbsolutePath();
        mItemDataList = new ArrayList<>();
        mItemDataAdapter = new ItemDataAdapter(this, 0, mItemDataList);
        updateListView();
        if (ActivityRequestPermissions.isRuntimePermissionSupported()) {
            String[] strArr = {"android.permission.WRITE_EXTERNAL_STORAGE"};
            ActivityRequestPermissions.Permission permission = new ActivityRequestPermissions.Permission(strArr[0], new String[]{getString(R.string.permission_dialog_title), getString(R.string.permission_dialog_title)}, new String[]{ActivityRequestPermissions.DialogParameter.setMessage2((Context) this, getString(R.string.permission_dialog_message_storage)), ActivityRequestPermissions.DialogParameter.setMessage3A((Context) this, getString(R.string.permission_dialog_message_storage), getString(R.string.permission_function_storage))});
            if (!ActivityRequestPermissions.checkPermission(this, strArr)) {
                ActivityRequestPermissions.requestPermission((Activity) this, permission, 1);
                return;
            }
        }
        initFolder();
    }

    private void initFolder() {
        try {
            mFolderInformation = new FolderInformation(mRootFolder, 0, mItemDataAdapter, mItemDataList, new CurrentFolderName(mFolderSelectCurrent, mFolderSelectText), mMenuControl);
            mFolderInformation.UpdateFolder();
        } catch (NullPointerException unused) {
            setResult(1, (Intent) null);
            finish();
        }
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 1) {
            if (i2 != -1) {
                setResult(0);
                finish();
                return;
            }
            initFolder();
        }
    }

    private void executeMessage(Message message) {
        if (message.what == MSG_UPDATE_LIST_VIEW) {
            updateListView();
        }
    }

    private void updateListView() {
        int i = 0;
        while (true) {
            if (i >= mFolderSelectBase.getChildCount()) {
                break;
            } else if (mFolderSelectBase.getChildAt(i).equals(mListView)) {
                mFolderSelectBase.removeView(mListView);
                break;
            } else {
                i++;
            }
        }
        mListView = new ListView(this);
        mListView.setAdapter(mItemDataAdapter);
        mListView.setOnItemClickListener(listViewListener);
        mFolderSelectBase.addView(mListView);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        mMenuControl.CreateMenu(menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != R.id.f337ok) {
            return super.onOptionsItemSelected(menuItem);
        }
        onActionbarMenuidOkClicked((View) null);
        return true;
    }

    public void onActionbarMenuidOkClicked(View view) {
        File GetCurrentFolder = mFolderInformation.GetCurrentFolder();
        Intent intent = new Intent();
        intent.putExtra("SELECT_Folder", GetCurrentFolder.getAbsolutePath());
        setResult(-1, intent);
        finish();
    }

    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
        super.onCreateContextMenu(contextMenu, view, contextMenuInfo);
        ListView listView = (ListView) view;
        if (mFolderInformation.IsFolder()) {
            contextMenu.add(0, 0, 0, "選択");
            contextMenu.add(0, 1, 0, "削除");
        }
    }

    public boolean onContextItemSelected(MenuItem menuItem) {
        menuItem.getItemId();
        return true;
    }
}
