package com.casic.dcms.ui;

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

import com.casic.dcms.adapter.ImageGridViewAdapter;
import com.casic.dcms.base.AndroidxBaseActivity;
import com.casic.dcms.bean.CaseDetailBean;
import com.casic.dcms.databinding.ActivityPackageHandledBinding;
import com.casic.dcms.mvp.presenter.impl.CaseDetailPresenterImpl;
import com.casic.dcms.mvp.view.ICaseDetailView;
import com.casic.dcms.utils.OtherUtils;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.ToastHelper;

import java.util.ArrayList;

public class PackageHandledDetailActivity extends AndroidxBaseActivity<ActivityPackageHandledBinding> implements ICaseDetailView {

    private final Context context = PackageHandledDetailActivity.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(context, data.getEorcName(), data.getLng(), data.getLat()));
        viewBinding.handleLayout.setOnClickListener(v -> OtherUtils.showCaseProcess(context, data.getProcessId()));
    }


    @Override
    public void obtainCaseDetailData(CaseDetailBean resultBean) {
        if (resultBean.getCode() == 200) {
            data = resultBean.getData().get(0);
            viewBinding.caseNumberView.setText(data.getCaseid());
            viewBinding.caseHandleStatusView.setText(data.getCaseStateName());
            viewBinding.caseTypeView.setText(data.getEorcName());
            viewBinding.caseLargeTypeView.setText(data.getCasetypeName());
            viewBinding.caseSmallTypeView.setText(data.getCasetypeDetailName());
            viewBinding.caseCommunityView.setText(data.getCommunityName().equals("") ? "无更详细社区信息" : 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);
                    }
                });
            }

            //处罚记录
            viewBinding.punishTypeView.setText(StringHelper.TypeToString(data.getPunishType()));
            viewBinding.punishOpinionView.setText(StringHelper.filterString(data.getPunishRemarks()));
            String punishImages = data.getPunishImage();
            if (!TextUtils.isEmpty(punishImages)) {
                ArrayList<String> urls = new ArrayList<>();
                String[] split = punishImages.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.punishGridView.setAdapter(imageAdapter);
                //九宫格点击事件
                viewBinding.punishGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                        OtherUtils.showBigImage(context, position, urls);
                    }
                });
            }
            viewBinding.punishDateView.setText(data.getPunishTime());
        }
    }

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

    @Override
    protected void onDestroy() {
        caseDetailPresenter.disposeRetrofitRequest();
        super.onDestroy();
    }
}