
package com.sobot.album.app.album;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.sobot.album.Action;
import com.sobot.album.R;
import com.sobot.album.SobotAlbum;
import com.sobot.album.api.widget.Widget;
import com.sobot.album.app.Contract;
import com.sobot.album.mvp.SobotAlbumBaseActivity;

public class NullActivity extends SobotAlbumBaseActivity implements Contract.NullPresenter {

    private static final String KEY_OUTPUT_IMAGE_PATH = "KEY_OUTPUT_IMAGE_PATH";

    public static String parsePath(Intent intent) {
        return intent.getStringExtra(KEY_OUTPUT_IMAGE_PATH);
    }

    private Widget mWidget;
    private int mQuality = 1;
    private long mLimitDuration;
    private long mLimitBytes;

    private Contract.NullView mView;


    @Override
    protected int getContentViewResId() {
        return R.layout.album_activity_null;
    }

    @Override
    protected void initView(){
        mView = new NullView(this, this);

        Bundle argument = getIntent().getExtras();
        assert argument != null;
        int function = argument.getInt(SobotAlbum.KEY_INPUT_FUNCTION);
        boolean hasCamera = argument.getBoolean(SobotAlbum.KEY_INPUT_ALLOW_CAMERA);

        mQuality = argument.getInt(SobotAlbum.KEY_INPUT_CAMERA_QUALITY);
        mLimitDuration = argument.getLong(SobotAlbum.KEY_INPUT_CAMERA_DURATION);
        mLimitBytes = argument.getLong(SobotAlbum.KEY_INPUT_CAMERA_BYTES);

        mWidget = argument.getParcelable(SobotAlbum.KEY_INPUT_WIDGET);
        mView.setupViews(mWidget);
        mView.setTitle(mWidget.getTitle());

        switch (function) {
            case SobotAlbum.FUNCTION_CHOICE_IMAGE: {
                mView.setMessage(R.string.album_not_found_image);
                mView.setMakeVideoDisplay(false);
                break;
            }
            case SobotAlbum.FUNCTION_CHOICE_VIDEO: {
                mView.setMessage(R.string.album_not_found_video);
                mView.setMakeImageDisplay(false);
                break;
            }
            case SobotAlbum.FUNCTION_CHOICE_ALBUM: {
                mView.setMessage(R.string.album_not_found_album);
                break;
            }
            default: {
                throw new AssertionError("This should not be the case.");
            }
        }

        if (!hasCamera) {
            mView.setMakeImageDisplay(false);
            mView.setMakeVideoDisplay(false);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void takePicture() {
        SobotAlbum.camera(this)
                .image()
                .onResult(mCameraAction)
                .start();
    }

    @Override
    public void takeVideo() {
        SobotAlbum.camera(this)
                .video()
                .quality(mQuality)
                .limitDuration(mLimitDuration)
                .limitBytes(mLimitBytes)
                .onResult(mCameraAction)
                .start();
    }

    private Action<String> mCameraAction = new Action<String>() {
        @Override
        public void onAction(@NonNull String result) {
            Intent intent = new Intent();
            intent.putExtra(KEY_OUTPUT_IMAGE_PATH, result);
            setResult(RESULT_OK, intent);
            finish();
        }
    };
}