/*
 * Copyright 2018 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.app.Activity;
import android.net.Uri;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

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

import com.sobot.album.AlbumFile;
import com.sobot.album.R;
import com.sobot.album.api.widget.Widget;
import com.sobot.album.app.Contract;
import com.sobot.album.app.fragment.ImageFragment;
import com.sobot.album.app.fragment.SobotViewPagerAdapter;
import com.sobot.album.app.fragment.VideoFragment;
import com.sobot.utils.SobotStringUtils;

import java.util.ArrayList;
import java.util.List;

public class PreviewView<Data> extends Contract.GalleryView<Data> implements View.OnClickListener {

    private Activity mActivity;

//    private MenuItem mCompleteMenu;

    private ViewPager mViewPager;
    private LinearLayout mLayoutBottom;
    private TextView mTvDuration;
    private FrameLayout mLayoutLayer;
    private SobotViewPagerAdapter pagerAdapter;
    private List<Fragment> fragments;
    private FragmentManager fragmentManager;


    public PreviewView(Activity activity, FragmentManager fragmentManager, Contract.GalleryPresenter presenter) {
        super(activity, presenter);
        this.mActivity = activity;
        this.fragmentManager = fragmentManager;
        this.mViewPager = activity.findViewById(R.id.view_pager);
        this.mLayoutBottom = activity.findViewById(R.id.layout_bottom);
        this.mTvDuration = activity.findViewById(R.id.tv_duration);
        this.mLayoutLayer = activity.findViewById(R.id.layout_layer);
        this.mLayoutLayer.setOnClickListener(this);
    }

    @Override
    protected void onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.album_menu_gallery, menu);
//        mCompleteMenu = menu.findItem(R.id.album_menu_finish);
    }

    @Override
    protected void onOptionsItemSelected(MenuItem item) {
//        int id = item.getItemId();
//        if (id == R.id.album_menu_finish) {
//            getPresenter().complete();
//        }
    }

    @Override
    public void setupViews(Widget widget, boolean checkable) {
        mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                getPresenter().onCurrentChanged(position);
            }
        });
    }

    @Override
    public void bindData(List<Data> dataList) {
        if(dataList==null ||dataList.size()==0){
            return;
        }
        fragments = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            if (dataList.get(i) instanceof AlbumFile) {
                AlbumFile albumFile = (AlbumFile) dataList.get(i);
                if (albumFile.getMediaType() == AlbumFile.TYPE_IMAGE) {
                    ImageFragment fragment = ImageFragment.getInstance(albumFile);
                    fragments.add(fragment);
                } else if (albumFile.getMediaType() == AlbumFile.TYPE_VIDEO) {
                    if(null==albumFile.getUri() && SobotStringUtils.isNoEmpty(albumFile.getPath())){
                        albumFile.setUri(Uri.parse(albumFile.getPath()));
                    }
                    VideoFragment fragment = VideoFragment.getInstance(albumFile);
                    fragments.add(fragment);
                }
            }
        }
        pagerAdapter = new SobotViewPagerAdapter(mActivity, fragmentManager, fragments);
        mViewPager.setAdapter(pagerAdapter);
    }

    @Override
    public void setCurrentItem(int position) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                mViewPager.setCurrentItem(position);
            }
        });

    }

    @Override
    public void setDurationDisplay(boolean display) {
        mTvDuration.setVisibility(display ? View.VISIBLE : View.GONE);
    }

    @Override
    public void setDuration(String duration) {
        mTvDuration.setText(duration);
    }

    @Override
    public void setChecked(boolean checked) {
    }

    @Override
    public void setBottomDisplay(boolean display) {
        mLayoutBottom.setVisibility(display ? View.VISIBLE : View.GONE);
    }

    @Override
    public void setLayerDisplay(boolean display) {
//        mLayoutLayer.setVisibility(display ? View.VISIBLE : View.GONE);
    }

    @Override
    public void setCompleteText(String sCheckedCount) {
    }

    @Override
    public void onClick(View v) {
        if (v == mLayoutLayer) {
            // Intercept click events.
        }
    }
}