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

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

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 PreviewAlbumActivity extends SobotAlbumBaseActivity implements Contract.GalleryPresenter, View.OnClickListener {


    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 Contract.GalleryView<AlbumFile> mView;
    private ArrayList<AlbumFile> mAlbumFiles;//预览的图片和视频
    private int mCurrentPosition;//当前页数

    private ViewPager mViewPager;
    private TextView sobot_tv_title;//预览已选时显示预览进度
    private FrameLayout mLayoutLayer;
    private FragmentManager fragmentManager;
    private TextView sobot_tv_page;//当前页数显示
    private TextView sobot_tv_download;//下载按钮


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

    @Override
    protected void initView() {
        mView = new PreviewView<>(this, getSupportFragmentManager(), this);
        this.fragmentManager = getSupportFragmentManager();
        this.mViewPager = findViewById(R.id.view_pager);
        this.mLayoutLayer = findViewById(R.id.layout_layer);
        this.sobot_tv_title = findViewById(R.id.sobot_text_title_center);
        this.sobot_tv_download = findViewById(R.id.sobot_tv_download);
        this.sobot_tv_page = findViewById(R.id.sobot_tv_page);
        this.sobot_tv_download.setOnClickListener(this);
        this.mLayoutLayer.setOnClickListener(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, 0);
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                onCurrentChanged(position);
                if (mAlbumFiles != null && position < mAlbumFiles.size()) {
                    showTitle(position);
                }
            }
        });
        mView.setupViews(mWidget, true);
        if (mCurrentPosition == 0) {
            onCurrentChanged(mCurrentPosition);
        } else {
            mView.setCurrentItem(mCurrentPosition);
        }
        mView.bindData(mAlbumFiles);
        setCheckedCount();
    }

    private void showTitle(int position) {
        String title = (position + 1) + "/" + mAlbumFiles.size();
        AlbumFile albumFile = mAlbumFiles.get(position);
        sobot_tv_page.setText(title);
        sobot_tv_page.setVisibility(View.VISIBLE);
        sobot_tv_title.setText(albumFile.getFileName());
        if (albumFile.getPath().startsWith("http")) {
            //显示下载
            sobot_tv_download.setVisibility(View.VISIBLE);
        } else {
            //隐藏下载
            sobot_tv_download.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initData() {
        //设置标题
        if (mAlbumFiles != null) {
            showTitle(mCurrentPosition);
        }
    }

    private void setCheckedCount() {
    }

    @Override
    public void clickItem(int position) {
    }

    @Override
    public void longClickItem(int position) {
    }

    @Override
    public void onCurrentChanged(int position) {
        mCurrentPosition = position;
    }

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

        setCheckedCount();
    }

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

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

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public void onClick(View view) {
        if (view == sobot_tv_download) {
            //下载
            AlbumFile albumFile = mAlbumFiles.get(mCurrentPosition);
            if(sClick!=null) {
                sClick.onAction(this, albumFile);
            }
        }
    }
}