package com.casic.dcms.ui;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.casic.dcms.adapter.ImageGridViewAdapter;
import com.casic.dcms.base.AndroidxBaseActivity;
import com.casic.dcms.bean.CaseDetailBean;
import com.casic.dcms.databinding.ActivityCaseDetailBinding;
import com.casic.dcms.mvp.presenter.impl.CaseDetailPresenterImpl;
import com.casic.dcms.mvp.view.ICaseDetailView;
import com.casic.dcms.utils.JzExoMedia;
import com.casic.dcms.utils.OtherUtils;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.ToastHelper;

import java.util.ArrayList;

import cn.jzvd.Jzvd;

public class CaseDetailActivity extends AndroidxBaseActivity<ActivityCaseDetailBinding> implements ICaseDetailView {

    private final Context context = CaseDetailActivity.this;
    private CaseDetailPresenterImpl caseDetailPresenter;
    private CaseDetailBean.DataBean data = new CaseDetailBean.DataBean();

    @Override
    protected void setupTopBarLayout() {
        viewBinding.titleBarLayout.titleView.setText("案卷详情");
        viewBinding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

    @Override
    protected void initData() {
        String id = getIntent().getStringExtra("id");
        caseDetailPresenter = new CaseDetailPresenterImpl(this);
        caseDetailPresenter.onReadyRetrofitRequest(id);
    }

    @Override
    protected void initEvent() {
        viewBinding.locationMapView.setOnClickListener(v -> OtherUtils.displayOnMap(this, data.getEorcName(), data.getLng(), data.getLat()));
        viewBinding.handleRecordLayout.setOnClickListener(v -> OtherUtils.showCaseProcess(this, data.getProcessId()));
    }

    @Override
    public void obtainCaseDetailData(CaseDetailBean resultBean) {
        if (resultBean.getCode() == 200) {
            data = resultBean.getData().get(0);
            viewBinding.caseNumberView.setText(data.getCaseid());
            viewBinding.caseStatusView.setText(data.getCaseStateName());
            viewBinding.caseTypeView.setText(data.getEorcName());
            viewBinding.caseLargeTypeView.setText(data.getCasetypeName());
            viewBinding.caseSmallTypeView.setText(data.getCasetypeDetailName());
            viewBinding.caseCommunityView.setText(data.getCommunityName());
            viewBinding.casePlaceView.setText(StringHelper.filterString(data.getFieldintro()));
            viewBinding.caseSituationView.setText(StringHelper.filterString(data.getDescription()));

            //核实图片
            String verifyImages = data.getFileIdVerify();
            if (!TextUtils.isEmpty(verifyImages)) {
                ArrayList<String> urls = new ArrayList<>();
                String[] split = verifyImages.split(",");
                for (String s : split) {
                    if (s.endsWith(".jpg") || s.contains(".jpeg") || s.contains(".png")) {
                        urls.add(StringHelper.appendCompleteURL(s));
                    }
                }
                ImageGridViewAdapter imageAdapter = new ImageGridViewAdapter(this);
                imageAdapter.setImageList(urls);
                viewBinding.casePicGridView.setAdapter(imageAdapter);
                //九宫格点击事件
                viewBinding.casePicGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        OtherUtils.showBigImage(context, position, urls);
                    }
                });
            }

            //处理图片
            String handleImages = data.getFileIdProcess();
            if (!TextUtils.isEmpty(handleImages)) {
                ArrayList<String> urls = new ArrayList<>();
                String[] split = handleImages.split(",");
                for (String s : split) {
                    if (s.contains("jpg") || s.contains("jpeg") || s.contains("png")) {
                        urls.add(StringHelper.appendCompleteURL(s));
                    }
                }
                ImageGridViewAdapter imageAdapter = new ImageGridViewAdapter(this);
                imageAdapter.setImageList(urls);
                viewBinding.handleGridView.setAdapter(imageAdapter);
                //九宫格点击事件
                viewBinding.handleGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        OtherUtils.showBigImage(context, position, urls);
                    }
                });
            }

            //核查图片
            String checkImages = data.getFileIdCheck();
            if (!TextUtils.isEmpty(checkImages)) {
                ArrayList<String> urls = new ArrayList<>();
                String[] split = checkImages.split(",");
                for (String s : split) {
                    if (s.contains("jpg") || s.contains("jpeg") || s.contains("png")) {
                        urls.add(StringHelper.appendCompleteURL(s));
                    }
                }
                ImageGridViewAdapter imageAdapter = new ImageGridViewAdapter(this);
                imageAdapter.setImageList(urls);
                viewBinding.checkGridView.setAdapter(imageAdapter);
                //九宫格点击事件
                viewBinding.checkGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        OtherUtils.showBigImage(context, position, urls);
                    }
                });
            }

            //音频
            String audioFile = data.getAudioIdVerify();
            if (!TextUtils.isEmpty(audioFile)) {
                viewBinding.audioPlayerView.setVisibility(View.VISIBLE);
                viewBinding.audioPlayerView.setAudioUrl(StringHelper.appendCompleteURL(audioFile));
            }

            //视频
            String videoFile = data.getVideoIdVerify();
            if (!TextUtils.isEmpty(videoFile)) {
                viewBinding.videoPlayerView.setVisibility(View.VISIBLE);
                String videoUrl = StringHelper.appendCompleteURL(videoFile);
                viewBinding.videoPlayerView.setUp(
                        videoUrl,
                        StringHelper.filterString(data.getDescription()),
                        Jzvd.SCREEN_NORMAL,
                        JzExoMedia.class
                );
                //设置第一帧为封面
                Glide.with(this)
                        .setDefaultRequestOptions(new RequestOptions().frame(4000000))
                        .load(videoUrl)
                        .into(viewBinding.videoPlayerView.posterImageView);
            }
        }
    }

    @Override
    public void obtainDataFail() {
        ToastHelper.showToast("获取案卷详情失败", ToastHelper.ERROR);
    }

    @Override
    public void onDestroy() {
        caseDetailPresenter.disposeRetrofitRequest();
        viewBinding.audioPlayerView.release();
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }
}