package com.wisewoods.eduandroid.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.danikula.videocache.HttpProxyCacheServer;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.wisewoods.base.base.BaseActivity;
import com.wisewoods.base.bean.BaseEvent;
import com.wisewoods.base.bean.BaseInfo;
import com.wisewoods.base.bean.UploadImgBean;
import com.wisewoods.base.net.JDCallback;
import com.wisewoods.base.utils.GlideEngine;
import com.wisewoods.base.utils.ImgUtils;
import com.wisewoods.eduandroid.R;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.databinding.ActivityBuildingBaseBinding;
import com.wisewoods.eduandroid.view.cache.ProxyVideoCacheManager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

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

import retrofit2.Response;
import xyz.doikki.videocontroller.StandardVideoController;
import xyz.doikki.videoplayer.player.VideoView;

public class BuildingBaseActivity extends BaseActivity<ActivityBuildingBaseBinding> {

    private String id;
    private List<LocalMedia> imgs;
    private List<VideoView> videos = new ArrayList<>();
    private BaseInfo.DataBean data;


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(BaseEvent.BaseInfoEvent event) {
        getData();
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    public static void start(String id) {
        Intent intent = new Intent(ActivityUtils.getTopActivity(), BuildingBaseActivity.class);
        intent.putExtra("id", id);
        ActivityUtils.startActivity(intent);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        showBarBack(binding.bar.tvTitle, "基地内容", binding.bar.ivBack);
        id = getIntent().getStringExtra("id");
        binding.tvChangeInfo.setOnClickListener(v -> {
            ChangeBuildingActivity.start(data);
        });
        getData();
    }

    private void getData() {
        App.api.getBaseInfo(id)
                .enqueue(new JDCallback<BaseInfo>() {

                    @Override
                    public void onS(Response<BaseInfo> response, BaseInfo baseInfo) {
                        data = baseInfo.data;
                        binding.tvName.setText(baseInfo.data.baseName);
                        binding.tvLocation.setText(baseInfo.data.baseRegion2);
                        binding.tvAddress.setText(baseInfo.data.baseLocation);
                        binding.tvPeople.setText(baseInfo.data.linkmanName);
                        binding.tvTel.setText(baseInfo.data.linkmanTel + "");
                        binding.tvCategory.setText(baseInfo.data.dictLabel);
                        binding.tvContent.setText(baseInfo.data.baseDesc);

                        imgs = new ArrayList<>();
                        binding.llImg.removeAllViews();
                        if (baseInfo.data.basePicture != null) {
                            for (int i = 0; i < baseInfo.data.basePicture.size(); i++) {
                                String url = baseInfo.data.basePicture.get(i);
                                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                        SizeUtils.dp2px(190));
                                params.topMargin = SizeUtils.dp2px(10);
                                binding.llImg.addView(createImg(url, i), params);
                                LocalMedia localMedia = new LocalMedia();
                                localMedia.setPath(url);
                                imgs.add(localMedia);
                            }
                        }

                        videos.clear();
                        binding.llVideo.removeAllViews();
                        if (baseInfo.data.baseVideo != null) {
                            for (UploadImgBean uploadImgBean : baseInfo.data.baseVideo) {
                                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                        SizeUtils.dp2px(190));
                                VideoView video = createVideo(uploadImgBean.url);
                                videos.add(video);
                                params.topMargin = SizeUtils.dp2px(10);
                                binding.llVideo.addView(video, params);
                            }
                        }
                    }
                });
    }

    private ImageView createImg(String url, int postion) {
        ImageView iv = new ImageView(this);
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ImgUtils.load(this, url, iv);
        iv.setOnClickListener(v -> {
            PictureSelector.create(this)
                    .themeStyle(R.style.picture_default_style)
                    .isNotPreviewDownload(true)
                    .imageEngine(GlideEngine.createGlideEngine())
                    .openExternalPreview(postion, imgs);
        });
        return iv;
    }

    private VideoView createVideo(String url) {
        VideoView player = new VideoView(this);
        HttpProxyCacheServer cacheServer = ProxyVideoCacheManager.getProxy(activity);
        String proxyUrl = cacheServer.getProxyUrl(url);
        player.setUrl(proxyUrl);
        StandardVideoController controller = new StandardVideoController(activity);
        controller.addDefaultControlComponent("", false);
        player.setVideoController(controller);
        return player;
    }

    @Override
    protected void onPause() {
        super.onPause();
        for (VideoView jzVideo : videos) {
            jzVideo.pause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        for (VideoView jzVideo : videos) {
            jzVideo.resume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        for (VideoView jzVideo : videos) {
            jzVideo.release();

        }
    }


    @Override
    public void onBackPressed() {
        boolean flag = true;
        for (VideoView jzVideo : videos) {
            if (jzVideo.onBackPressed()) {
                flag =false;
            }
        }
        if (flag){
            super.onBackPressed();
        }

    }
}
