/*
 * Copyright 2017 Yan Zhenjie.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sobot.album.app.album;

import android.os.Bundle;

import androidx.annotation.Nullable;

import com.sobot.album.AlbumFile;
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.app.gallery.GalleryView;
import com.sobot.album.util.AlbumUtils;
import com.sobot.widget.ui.base.SobotBaseActivity;

import java.util.ArrayList;

/**
 * <p>Preview the pictures in the folder in enlarged form.</p>
 * Created by Yan Zhenjie on 2017/3/25.
 * 预览
 */
public class GalleryActivity extends SobotBaseActivity implements Contract.GalleryPresenter {

    public static ArrayList<AlbumFile> sAlbumFiles;
    public static int sCheckedCount;
    public static int sCurrentPosition;

    public static Callback sCallback;
    public static boolean previewMySelect;

    private Widget mWidget;
    private int mFunction;
    private int mAllowSelectCount;

    private Contract.GalleryView<AlbumFile> mView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        changeAppLanguage();
        super.onCreate(savedInstanceState);
    }

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

    @Override
    protected void initView(){
        mView = new GalleryView<>(this, getSupportFragmentManager(),this);
        Bundle argument = getIntent().getExtras();
        assert argument != null;
        mWidget = argument.getParcelable(SobotAlbum.KEY_INPUT_WIDGET);
        mFunction = argument.getInt(SobotAlbum.KEY_INPUT_FUNCTION);
        mAllowSelectCount = argument.getInt(SobotAlbum.KEY_INPUT_LIMIT_COUNT);
        mView.setShowTitle(previewMySelect);
        mView.setupViews(mWidget, true);
        mView.bindData(sAlbumFiles);

        if (sCurrentPosition == 0) {
            onCurrentChanged(sCurrentPosition);
        } else {
            mView.setCurrentItem(sCurrentPosition);
        }
        setCheckedCount();
    }

    @Override
    protected void initData() {

    }

    private void setCheckedCount() {
        //显示已选
        mView.setCompleteText(String.valueOf(sCheckedCount));
    }

    @Override
    public void clickItem(int position) {
    }

    @Override
    public void longClickItem(int position) {
    }

    @Override
    public void onCurrentChanged(int position) {
        sCurrentPosition = position;
        mView.setTitle(sCurrentPosition + 1 + " / " + sAlbumFiles.size());

        AlbumFile albumFile = sAlbumFiles.get(position);
        mView.setChecked(albumFile.isChecked());
        mView.setLayerDisplay(albumFile.isDisable());

//        if (albumFile.getMediaType() == AlbumFile.TYPE_VIDEO) {
//            mView.setDuration(AlbumUtils.convertDuration(albumFile.getDuration()));
//            mView.setDurationDisplay(true);
//        } else {
//            mView.setDurationDisplay(false);
//        }
    }

    @Override
    public void onCheckedChanged() {
        AlbumFile albumFile = sAlbumFiles.get(sCurrentPosition);
        if (albumFile.isChecked()) {
            albumFile.setChecked(false);
            sCallback.onPreviewChanged(albumFile);
            sCheckedCount--;
        } else {
            if (sCheckedCount >= mAllowSelectCount) {
                int messageRes;
                switch (mFunction) {
                    case SobotAlbum.FUNCTION_CHOICE_IMAGE: {
                        messageRes = R.string.album_check_image_limit;
                        break;
                    }
                    case SobotAlbum.FUNCTION_CHOICE_VIDEO: {
                        messageRes = R.string.album_check_video_limit;
                        break;
                    }
                    case SobotAlbum.FUNCTION_CHOICE_ALBUM: {
                        messageRes = R.string.album_check_album_limit;
                        break;
                    }
                    default: {
                        throw new AssertionError("This should not be the case.");
                    }
                }
                String text = getResources().getText(messageRes).toString();
                mView.toast(String.format(text, mAllowSelectCount));
                mView.setChecked(false);
            } else if(albumFile.getSize()>= 50 * 1024 * 1024){
                mView.toast(getResources().getString(R.string.sobot_pic_siza_xiaoyu) );
                mView.setChecked(false);
            } else {
                albumFile.setChecked(true);
                sCallback.onPreviewChanged(albumFile);
                sCheckedCount++;
            }
        }

        setCheckedCount();
    }

    @Override
    public void complete() {
        if (sCheckedCount == 0) {
            int messageRes;
            switch (mFunction) {
                case SobotAlbum.FUNCTION_CHOICE_IMAGE: {
                    messageRes = R.string.sobot_album_check_image_little;
                    break;
                }
                case SobotAlbum.FUNCTION_CHOICE_VIDEO: {
                    messageRes = R.string.sobot_album_check_video_little;
                    break;
                }
                case SobotAlbum.FUNCTION_CHOICE_ALBUM: {
                    messageRes = R.string.sobot_album_check_album_little;
                    break;
                }
                default: {
                    throw new AssertionError("This should not be the case.");
                }
            }
            mView.toast(messageRes);
        } else {
            sCallback.onPreviewComplete();
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    public void finish() {
        sAlbumFiles = null;
        sCheckedCount = 0;
        sCurrentPosition = 0;
        sCallback = null;
        super.finish();
    }

    @Override
    public void bye() {

    }

    public interface Callback {

        /**
         * Complete the preview.
         */
        void onPreviewComplete();

        /**
         * Check or uncheck a item.
         *
         * @param albumFile target item.
         */
        void onPreviewChanged(AlbumFile albumFile);
    }
}