/*
 * 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.gallery;

import android.os.Bundle;

import com.sobot.album.Action;
import com.sobot.album.AlbumFile;
import com.sobot.album.ItemAction;
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;

import java.util.ArrayList;

public class GalleryAlbumActivity extends SobotAlbumBaseActivity implements Contract.GalleryPresenter {

    public static Action<ArrayList<AlbumFile>> sResult;
    public static Action<String> sCancel;

    public static ItemAction<AlbumFile> sClick;
    public static ItemAction<AlbumFile> sLongClick;

    private Widget mWidget;
    private ArrayList<AlbumFile> mAlbumFiles;
    private int mCurrentPosition;
    private boolean mCheckable;

    private Contract.GalleryView<AlbumFile> mView;


    @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);
        mAlbumFiles = argument.getParcelableArrayList(SobotAlbum.KEY_INPUT_CHECKED_LIST);
        mCurrentPosition = argument.getInt(SobotAlbum.KEY_INPUT_CURRENT_POSITION);
        mCheckable = argument.getBoolean(SobotAlbum.KEY_INPUT_GALLERY_CHECKABLE);

        mView.setTitle(mWidget.getTitle());
        mView.setupViews(mWidget, mCheckable);
        mView.bindData(mAlbumFiles);
        if (mCurrentPosition == 0) {
            onCurrentChanged(mCurrentPosition);
        } else {
            mView.setCurrentItem(mCurrentPosition);
        }
        setCheckedCount();
    }

    @Override
    protected void initData() {

    }

    private void setCheckedCount() {
        int checkedCount = 0;
        for (AlbumFile albumFile : mAlbumFiles) {
            if (albumFile.isChecked()) checkedCount += 1;
        }

//        String completeText = getString(R.string.sobot_album_menu_finish);
//        completeText += "(" + checkedCount + " / " + mAlbumFiles.size() + ")";
//        mView.setCompleteText(completeText);
        mView.setCompleteText(String.valueOf(checkedCount));
    }

    @Override
    public void clickItem(int position) {
        if (sClick != null) {
            sClick.onAction(GalleryAlbumActivity.this, mAlbumFiles.get(mCurrentPosition));
        }
    }

    @Override
    public void longClickItem(int position) {
        if (sLongClick != null) {
            sLongClick.onAction(GalleryAlbumActivity.this, mAlbumFiles.get(mCurrentPosition));
        }
    }

    @Override
    public void onCurrentChanged(int position) {
        mCurrentPosition = position;
        mView.setSubTitle(position + 1 + " / " + mAlbumFiles.size());

        AlbumFile albumFile = mAlbumFiles.get(position);
        if (mCheckable) mView.setChecked(albumFile.isChecked());
        mView.setLayerDisplay(albumFile.isDisable());

        //多语言显示不下，不显示视频秒数
//        if (albumFile.getMediaType() == AlbumFile.TYPE_VIDEO) {
//            if (!mCheckable) mView.setBottomDisplay(true);
//            mView.setDuration(AlbumUtils.convertDuration(albumFile.getDuration()));
//            mView.setDurationDisplay(true);
//        } else {
//            if (!mCheckable) mView.setBottomDisplay(false);
//            mView.setDurationDisplay(false);
//        }
    }

    @Override
    public void onCheckedChanged() {
        AlbumFile albumFile = mAlbumFiles.get(mCurrentPosition);
        albumFile.setChecked(!albumFile.isChecked());

        setCheckedCount();
    }

    @Override
    public void complete() {
        if (sResult != null) {
            ArrayList<AlbumFile> checkedList = new ArrayList<>();
            for (AlbumFile albumFile : mAlbumFiles) {
                if (albumFile.isChecked()) checkedList.add(albumFile);
            }
            sResult.onAction(checkedList);
        }
        finish();
    }

    @Override
    public void onBackPressed() {
        if (sCancel != null) sCancel.onAction("User canceled.");
        finish();
    }

    @Override
    public void finish() {
        sResult = null;
        sCancel = null;
        sClick = null;
        sLongClick = null;
        super.finish();
    }
}