package com.datarecovery.master.module.preview;

import android.net.Uri;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.atmob.app.lib.base.BaseViewModel;
import com.atmob.common.runtime.ContextUtil;
import com.datarecovery.master.R;
import com.datarecovery.master.data.repositories.DeviceFuncRepository;
import com.datarecovery.master.module.imgrecover.ImageRecoverActivity;
import com.datarecovery.master.module.member.MemberType;
import com.datarecovery.master.utils.BoxingUtil;
import com.datarecovery.master.utils.FileUtil;
import com.datarecovery.master.utils.FilesSearch;
import com.datarecovery.master.utils.ImageDeepDetector;
import com.datarecovery.master.utils.MediaStoreHelper;
import com.datarecovery.master.utils.ToastUtil;

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.concurrent.Callable;

import javax.inject.Inject;

import atmob.rxjava.utils.RxJavaUtil;
import dagger.hilt.android.lifecycle.HiltViewModel;

@HiltViewModel
public class PreviewViewModel extends BaseViewModel {

    public static WeakReference<?> weakReference;
    private final MutableLiveData<String> title = new MutableLiveData<>();
    private final MutableLiveData<Uri> previewUri = new MutableLiveData<>();
    private final MutableLiveData<Boolean> isPlaying = new MutableLiveData<>();
    private final DeviceFuncRepository deviceFuncRepository;
    private List<ImageDeepDetector.ImageFile> imagePreviewList;

    private int type;

    private final MutableLiveData<Boolean> isPlayStart = new MutableLiveData<>();
    private final MutableLiveData<ImageDeepDetector.ImageFile> currentImageFile = new MutableLiveData<>();
    private FilesSearch.DocumentFile currentFileFile;
    private int position;


    @Inject
    public PreviewViewModel(DeviceFuncRepository deviceFuncRepository) {
        this.deviceFuncRepository = deviceFuncRepository;
    }

    public MutableLiveData<Boolean> getIsPlayStart() {
        return isPlayStart;
    }

    public LiveData<ImageDeepDetector.ImageFile> getCurrentImageFile() {
        return currentImageFile;
    }

    public int getPosition() {
        return position;
    }

    public List<ImageDeepDetector.ImageFile> getImagePreviewList() {
        return imagePreviewList;
    }

    public LiveData<String> getTitle() {
        return title;
    }

    public int getType() {
        return type;
    }

    public LiveData<Uri> getPreviewUri() {
        return previewUri;
    }

    public void setIsPlaying(boolean isPlaying) {
        this.isPlaying.setValue(isPlaying);
    }

    public LiveData<Boolean> getIsPlaying() {
        return isPlaying;
    }

    public void setPreviewData(@PreviewActivity.Type int type, int position) {
        this.type = type;
        this.position = position;
        switch (type) {
            case PreviewActivity.TYPE_AUDIO:
                dealAudioInit();
                break;
            case PreviewActivity.TYPE_IMG:
                dealImageInit();
                break;
            case PreviewActivity.TYPE_VIDEO:
                dealVideoInit();
                break;
        }
    }

    private void dealVideoInit() {
        title.setValue(ContextUtil.getContext().getString(R.string.preview_video));
        if (weakReference != null) {
            this.currentFileFile = (FilesSearch.DocumentFile) weakReference.get();
            setUri(currentFileFile.getUri());
        }
    }

    private void dealAudioInit() {
        title.setValue(ContextUtil.getContext().getString(R.string.preview_audio));
        if (weakReference != null) {
            this.currentFileFile = (FilesSearch.DocumentFile) weakReference.get();
            setUri(currentFileFile.getUri());
        }
    }

    public Uri getUri() {
        if (type == PreviewActivity.TYPE_IMG) {
            ImageDeepDetector.ImageFile value = currentImageFile.getValue();
            if (value != null) {
                return value.getUri();
            } else {
                return null;
            }
        } else if (type == PreviewActivity.TYPE_AUDIO || type == PreviewActivity.TYPE_VIDEO) {
            if (currentFileFile != null) {
                return currentFileFile.getUri();
            } else {
                return null;
            }
        }
        return null;
    }

    private void dealImageInit() {
        title.setValue(ContextUtil.getContext().getString(R.string.preview_img));
        if (weakReference != null) {
            this.imagePreviewList = (List<ImageDeepDetector.ImageFile>) weakReference.get();
        }
    }


    public void setUri(Uri uri) {
        this.previewUri.setValue(uri);
    }

    public void setIsPlayStart() {
        if (BoxingUtil.boxing(this.isPlayStart.getValue())) {
            return;
        }
        this.isPlayStart.setValue(true);
    }


    public void setPosition(int position) {
        if (type == PreviewActivity.TYPE_IMG) {
            if (position != -1 && imagePreviewList != null && position < imagePreviewList.size()) {
                ImageDeepDetector.ImageFile imageFile = imagePreviewList.get(position);
                currentImageFile.setValue(imageFile);
            }

        }
    }

    public void onExportClick() {
        if (type == ImageRecoverActivity.Type.RECOVER && !deviceFuncRepository.isHaveAuth(MemberType.APP_IMAGE_RECOVER)) {
            return;
        }
        RxJavaUtil.doInBackground(() -> {
                    if (type == PreviewActivity.TYPE_IMG) {
                        ImageDeepDetector.ImageFile value = currentImageFile.getValue();
                        if (value == null) {
                            return false;
                        }
                        MediaStoreHelper.saveToSharedStorage(MediaStoreHelper.TYPE_IMAGE, value.newInputStream(), FileUtil.getExportImageFileName(value.getName()));
                    } else if (type == PreviewActivity.TYPE_AUDIO) {
                        if (currentFileFile == null) {
                            return false;
                        }
                        MediaStoreHelper.saveToSharedStorage(MediaStoreHelper.TYPE_AUDIO, currentFileFile.newInputStream(), FileUtil.getCreateFileName(currentFileFile.getName(), ""));
                    }
                    return true;
                }
                , o -> ToastUtil.show(R.string.export_success, ToastUtil.LENGTH_SHORT)
                , throwable -> ToastUtil.show(R.string.export_fail, ToastUtil.LENGTH_SHORT));

    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (weakReference != null) {
            weakReference.clear();
        }
    }
}