package com.epson.memcardacc;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

public class ImageFileListAdapter extends BaseAdapter implements Serializable {
    private static final int FILE_SELECT_NUMBER_LIMIT = 30;
    private static final long serialVersionUID = 1;
    /* access modifiers changed from: private */
    public String mBaseDir = "";
    LinkedList<FileInfoWithCheck> mFileInfoWithCheck = new LinkedList<>();
    private transient LayoutInflater mLayoutInflator;
    private AbsListView.LayoutParams mLayoutParams;
    ReaderSelectedImage mReaderSelectedImage = new ReaderSelectedImage();
    int mReqHeight;
    int mReqWidth;
    private String mUpFolderString = "";

    public long getItemId(int i) {
        return (long) i;
    }

    public void setLayoutInflater(LayoutInflater layoutInflater) {
        this.mLayoutInflator = layoutInflater;
    }

    public void setUpFolderString(String str) {
        this.mUpFolderString = str;
    }

    public int getCount() {
        return this.mFileInfoWithCheck.size();
    }

    public Object getItem(int i) {
        return this.mFileInfoWithCheck.get(i);
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = this.mLayoutInflator.inflate(R.layout.memcard_card_grid_view_elem, viewGroup, false);
        }
        ImageView imageView = (ImageView) view.findViewById(R.id.thumbNail);
        FileInfoWithCheck fileInfoWithCheck = this.mFileInfoWithCheck.get(i);
        Bitmap thumNail = fileInfoWithCheck.getThumNail();
        TextView textView = (TextView) view.findViewById(R.id.baseDirText);
        if (thumNail != null) {
            imageView.setImageBitmap(thumNail);
            textView.setVisibility(4);
        } else if (fileInfoWithCheck.isDirectory()) {
            if (fileInfoWithCheck.isParentDirectory()) {
                imageView.setImageResource(R.C2136drawable.parent_folder);
                String str = this.mUpFolderString;
                if (str != null) {
                    textView.setText(str);
                }
            } else {
                imageView.setImageResource(R.C2136drawable.folder);
                textView.setText(fileInfoWithCheck.getFileName());
            }
            textView.setVisibility(0);
        } else {
            imageView.setImageResource(R.C2136drawable.image);
            textView.setVisibility(4);
        }
        ImageView imageView2 = (ImageView) view.findViewById(R.id.checkImage);
        if (fileInfoWithCheck.mChecked != 0) {
            imageView2.setVisibility(0);
        } else {
            imageView2.setVisibility(8);
        }
        return view;
    }

    public void setImageSize(int i, int i2) {
        this.mReqWidth = i;
        this.mReqHeight = i2;
    }

    public void setFileList(String str, LinkedList<CifsFileInfo> linkedList) {
        String str2 = str.length() <= 0 ? "\\" : str;
        if (!this.mBaseDir.equals(str2)) {
            this.mReaderSelectedImage.setFileList(this.mBaseDir, getSelectedFileList());
            this.mBaseDir = str2;
            this.mFileInfoWithCheck.clear();
            if (!"\\".equals(this.mBaseDir)) {
                this.mFileInfoWithCheck.add(new FileInfoWithCheck(CifsAccess.getParentDirectory(str)));
            }
            Iterator it = linkedList.iterator();
            while (it.hasNext()) {
                CifsFileInfo cifsFileInfo = (CifsFileInfo) it.next();
                if (cifsFileInfo.isReadable()) {
                    FileInfoWithCheck fileInfoWithCheck = new FileInfoWithCheck(cifsFileInfo);
                    if (this.mReaderSelectedImage.contains(this.mBaseDir, cifsFileInfo)) {
                        fileInfoWithCheck.mChecked = 1;
                    }
                    this.mFileInfoWithCheck.add(fileInfoWithCheck);
                }
            }
            this.mReaderSelectedImage.deleteBaseDir(this.mBaseDir);
        }
    }

    private LinkedList<CifsFileInfo> getSelectedFileList() {
        LinkedList<CifsFileInfo> linkedList = new LinkedList<>();
        Iterator it = this.mFileInfoWithCheck.iterator();
        while (it.hasNext()) {
            FileInfoWithCheck fileInfoWithCheck = (FileInfoWithCheck) it.next();
            if (fileInfoWithCheck.mChecked != 0) {
                linkedList.add(fileInfoWithCheck.mCifsFileInfo);
            }
        }
        return linkedList;
    }

    public int getSelectedFileNum() {
        Iterator it = this.mFileInfoWithCheck.iterator();
        int i = 0;
        while (it.hasNext()) {
            if (((FileInfoWithCheck) it.next()).mChecked != 0) {
                i++;
            }
        }
        return i + this.mReaderSelectedImage.getFileSize();
    }

    public void clear() {
        this.mFileInfoWithCheck.clear();
        notifyDataSetChanged();
    }

    public Iterator<FileInfoWithCheck> getFileInfoIterator() {
        return this.mFileInfoWithCheck.iterator();
    }

    public String getDirectoryName(int i) {
        return ((FileInfoWithCheck) getItem(i)).getDirectoryName();
    }

    public ArrayList<String> getAllSelectedFile() {
        ArrayList<String> arrayList = new ArrayList<>();
        Iterator it = this.mFileInfoWithCheck.iterator();
        while (it.hasNext()) {
            FileInfoWithCheck fileInfoWithCheck = (FileInfoWithCheck) it.next();
            if (fileInfoWithCheck.mChecked != 0) {
                arrayList.add(fileInfoWithCheck.getFullPath());
            }
        }
        arrayList.addAll(this.mReaderSelectedImage.getAllFileFullPath());
        return arrayList;
    }

    public boolean itemSelected(int i) {
        FileInfoWithCheck fileInfoWithCheck = (FileInfoWithCheck) getItem(i);
        if (fileInfoWithCheck.mChecked == 0 && getSelectedFileNum() >= 30) {
            return false;
        }
        fileInfoWithCheck.toggleCheck();
        notifyDataSetChanged();
        return true;
    }

    public class FileInfoWithCheck {
        public int mChecked;
        /* access modifiers changed from: private */
        public CifsFileInfo mCifsFileInfo;
        String mParentDirectory;
        public Bitmap mThumbNail;

        public FileInfoWithCheck(CifsFileInfo cifsFileInfo) {
            this.mCifsFileInfo = cifsFileInfo;
        }

        public boolean isParentDirectory() {
            return this.mParentDirectory != null;
        }

        public Bitmap getThumNail() {
            return this.mThumbNail;
        }

        public String getFileName() {
            String str = this.mParentDirectory;
            if (str != null) {
                return str;
            }
            CifsFileInfo cifsFileInfo = this.mCifsFileInfo;
            if (cifsFileInfo != null) {
                return cifsFileInfo.mFileName;
            }
            throw new RuntimeException("item don't have CifsFileInfo");
        }

        public String getFullPath() {
            String str = this.mParentDirectory;
            if (str != null) {
                return str;
            }
            if (ImageFileListAdapter.this.mBaseDir.endsWith("\\")) {
                return ImageFileListAdapter.this.mBaseDir + this.mCifsFileInfo.mFileName;
            }
            return ImageFileListAdapter.this.mBaseDir + "\\" + this.mCifsFileInfo.mFileName;
        }

        public void setThumbNail(Bitmap bitmap) {
            this.mThumbNail = bitmap;
        }

        public boolean needThumbNail() {
            return this.mThumbNail == null;
        }

        public boolean isDirectory() {
            if (this.mParentDirectory != null) {
                return true;
            }
            return this.mCifsFileInfo.isDirectory();
        }

        public FileInfoWithCheck(String str) {
            this.mParentDirectory = str;
        }

        public String getDirectoryName() {
            String str = this.mParentDirectory;
            if (str != null) {
                return str;
            }
            if (!this.mCifsFileInfo.isDirectory()) {
                return null;
            }
            if (ImageFileListAdapter.this.mBaseDir.endsWith("\\")) {
                return ImageFileListAdapter.this.mBaseDir + this.mCifsFileInfo.mFileName;
            }
            return ImageFileListAdapter.this.mBaseDir + "\\" + this.mCifsFileInfo.mFileName;
        }

        public void toggleCheck() {
            if (this.mChecked == 0) {
                this.mChecked = 1;
            } else {
                this.mChecked = 0;
            }
        }
    }

    public String getTargetDirectoryName() {
        return this.mBaseDir;
    }
}
