package com.epson.memcardacc;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData;
import android.os.AsyncTask;

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

import epson.common.ExternalFileUtils;
import epson.common.ImageUtil;

public class MemCardWriteConvertViewModel extends AndroidViewModel {
    private final Application mApplication;
    private final ConvertLiveData mConvertLiveData = new ConvertLiveData();

    public static class ConvertResult {
        public final ArrayList<String> filenameList = new ArrayList<>();
        public long totalFileSize = 0;

        public void addFile(String str) {
            totalFileSize += new File(str).length();
            filenameList.add(str);
        }
    }

    public MemCardWriteConvertViewModel(@NonNull Application application) {
        super(application);
        mApplication = application;
    }

    public void setOrigFileList(@NonNull ArrayList<String> arrayList) {
        mConvertLiveData.setOrigFileList(arrayList);
    }

    public void deleteTempFile() {
        mConvertLiveData.initTempDirectory();
    }

    public ConvertLiveData getData() {
        return mConvertLiveData;
    }

    public class ConvertLiveData extends LiveData<ConvertResult> {
        public ConvertLiveData() {
        }

        public void setOrigFileList(@NonNull final ArrayList<String> arrayList) {
            new AsyncTask<Void, Void, ConvertResult>() {
                protected ConvertResult doInBackground(Void... voidArr) {
                    return ConvertLiveData.this.convertAndGetFileSize(arrayList);
                }

                protected void onPostExecute(ConvertResult convertResult) {
                    MemCardWriteConvertViewModel.mConvertLiveData.setValue(convertResult);
                }
            }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
        }

        private synchronized ConvertResult convertAndGetFileSize(@NonNull ArrayList<String> arrayList) {
            ConvertResult convertResult = new ConvertResult();
            File initTempDirectory = initTempDirectory(ExternalFileUtils.getInstance(MemCardWriteConvertViewModel.mApplication));
            Iterator<String> it = arrayList.iterator();
            while (it.hasNext()) {
                String next = it.next();
                if (ImageUtil.isHEIFFormat(next)) {
                    String notDuplicateFilename = ExternalFileUtils.getNotDuplicateFilename(next, initTempDirectory.getPath(), 30);
                    if (!ImageUtil.convertHEIFtoJPEG(next, notDuplicateFilename)) {
                        return null;
                    }
                    next = notDuplicateFilename;
                }
                convertResult.addFile(next);
            }
            return convertResult;
        }

        @NonNull
        private File initTempDirectory(ExternalFileUtils externalFileUtils) {
            return externalFileUtils.initTempCacheDirectory(ExternalFileUtils.TempCacheDirectory.MEMORY_CARD_ACCESS);
        }

        private synchronized void initTempDirectory() {
            initTempDirectory(ExternalFileUtils.getInstance(MemCardWriteConvertViewModel.mApplication));
        }
    }
}
