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;
    private 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) {
        mLayoutInflator = layoutInflater;
    }

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

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

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

    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = mLayoutInflator.inflate(R.layout.memcard_card_grid_view_elem, viewGroup, false);
        }
        ImageView imageView = (ImageView) view.findViewById(R.id.thumbNail);
        FileInfoWithCheck fileInfoWithCheck = 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.drawable.parent_folder);
                String str = mUpFolderString;
                if (str != null) {
                    textView.setText(str);
                }
            } else {
                imageView.setImageResource(R.drawable.folder);
                textView.setText(fileInfoWithCheck.getFileName());
            }
            textView.setVisibility(View.VISIBLE);
        } else {
            imageView.setImageResource(R.drawable.image);
            textView.setVisibility(4);
        }
        ImageView imageView2 = (ImageView) view.findViewById(R.id.checkImage);
        if (fileInfoWithCheck.mChecked != 0) {
            imageView2.setVisibility(View.VISIBLE);
        } else {
            imageView2.setVisibility(View.GONE);
        }
        return view;
    }

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

    public void setFileList(String str, LinkedList<CifsFileInfo> linkedList) {
        String str2 = str.length() <= 0 ? "\\" : str;
        if (!mBaseDir.equals(str2)) {
            mReaderSelectedImage.setFileList(mBaseDir, getSelectedFileList());
            mBaseDir = str2;
            mFileInfoWithCheck.clear();
            if (!"\\".equals(mBaseDir)) {
                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 (mReaderSelectedImage.contains(mBaseDir, cifsFileInfo)) {
                        fileInfoWithCheck.mChecked = 1;
                    }
                    mFileInfoWithCheck.add(fileInfoWithCheck);
                }
            }
            mReaderSelectedImage.deleteBaseDir(mBaseDir);
        }
    }

    private LinkedList<CifsFileInfo> getSelectedFileList() {
        LinkedList<CifsFileInfo> linkedList = new LinkedList<>();
        Iterator it = 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 = mFileInfoWithCheck.iterator();
        int i = 0;
        while (it.hasNext()) {
            if (((FileInfoWithCheck) it.next()).mChecked != 0) {
                i++;
            }
        }
        return i + mReaderSelectedImage.getFileSize();
    }

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

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

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

    public ArrayList<String> getAllSelectedFile() {
        ArrayList<String> arrayList = new ArrayList<>();
        Iterator it = mFileInfoWithCheck.iterator();
        while (it.hasNext()) {
            FileInfoWithCheck fileInfoWithCheck = (FileInfoWithCheck) it.next();
            if (fileInfoWithCheck.mChecked != 0) {
                arrayList.add(fileInfoWithCheck.getFullPath());
            }
        }
        arrayList.addAll(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;
        private CifsFileInfo mCifsFileInfo;
        String mParentDirectory;
        public Bitmap mThumbNail;

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

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

        public Bitmap getThumNail() {
            return mThumbNail;
        }

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

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

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

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

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

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

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

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

    public String getTargetDirectoryName() {
        return mBaseDir;
    }
}
