package com.sheep.gamegroup.module.find.activity;


import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.absBase.IRefresh;
import com.sheep.gamegroup.module.find.fragment.FgtMediaPickerImg;
import com.sheep.gamegroup.module.find.fragment.FgtMediaPickerVideo;
import com.sheep.gamegroup.model.entity.Video;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.TitleFragmentListAdapter;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import cn.finalteam.rxgalleryfinal.api.CameraCallBack;
import cn.finalteam.rxgalleryfinal.utils.MediaUtils;

/**
 * Created by realicing on 2018/11/26.
 * realicing@sina.com
 * 小绵羊3.4.5新增--发布发现内容--选择视频和图片
 */
public class ActMediaChoose extends BaseActivity {

    @Override
    protected int getLayoutId() {
        return R.layout.act_media_choose;
    }

    @BindView(R.id.indicator)
    TabLayout indicator;
    @BindView(R.id.below_tab_line)
    View below_tab_line;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    private TitleFragmentListAdapter mAdapter;
    @Override
    public void initView() {
        TitleBarUtils.getInstance()
                .setShowOrHide(this, true)
                .setTitle(this, "选择媒体")
                .setTitleFinish(this)
                .setRightImgBotton(this, R.mipmap.ic_open_camera, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Jump2View.getInstance().goActEditVideo(video);
                boolean isVideo = mAdapter.getItem(viewPager.getCurrentItem()) instanceof FgtMediaPickerVideo;
                MediaUtils.openCamera(ActMediaChoose.this, !isVideo);
            }
        });

        mAdapter = new TitleFragmentListAdapter(getSupportFragmentManager());
        mAdapter.add(new FgtMediaPickerVideo(), "视频");
        if(TestUtil.isDev()) {
            mAdapter.add(new FgtMediaPickerImg(), "图片");
        }
        viewPager.setAdapter(mAdapter);
        indicator.setupWithViewPager(viewPager);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(mAdapter.getCount());
        CommonUtil.getInstance().reflex(indicator, this);
        if(mAdapter.getCount() < 2){
            ViewUtil.setVisibility(indicator, false);
            ViewUtil.setVisibility(below_tab_line, false);
        }
//        Integer type = DataUtil.getObject(getIntent(), Integer.class);
//        if(type != null){
//            switch (type){
//                case MEDIA_TYPE_IMAGE:
//                    break;
//                case MEDIA_TYPE_VIDEO:
//                    break;
//                case MEDIA_TYPE_NONE:
//                default:
//                    break;
//            }
//        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK){
            MediaUtils.onActivityResult(this, requestCode, null, new CameraCallBack() {
                @Override
                public void onGetImageSuccess(String cropFilePath) {
                    if(mAdapter.getItem(1) instanceof IRefresh){
                        ((IRefresh) mAdapter.getItem(1)).refreshData();
                    }
                }

                @Override
                public void onGetVideoSuccess() {
                    if(mAdapter.getItem(0) instanceof IRefresh){
                        ((IRefresh) mAdapter.getItem(0)).refreshData();
                    }
                }

                @Override
                public void onCropImageSuccess() {
                    if(mAdapter.getItem(1) instanceof IRefresh){
                        ((IRefresh) mAdapter.getItem(1)).refreshData();
                    }
                }
            });
        }
    }
    @Override
    protected boolean needRegisterEventBus() {
        return true;
    }

    @Subscribe
    public void onEventMainThread(Video data) {
        if(data != null){
            finish();
        }
    }
}
