package com.casic.dcms.view.sanbao;

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

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.casic.dcms.R;
import com.casic.dcms.databinding.ActivitySanBaoHandledBinding;
import com.casic.dcms.model.CaseDetailBean;
import com.casic.dcms.utils.MapHelper;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.ViewGroupKit;
import com.casic.dcms.view.BigImageActivity;
import com.casic.dcms.view.CaseProcessActivity;
import com.casic.dcms.vm.CaseViewModel;
import com.pengxh.androidx.lite.adapter.ReadOnlyImageAdapter;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.kit.ContextKit;
import com.pengxh.androidx.lite.kit.StringKit;
import com.pengxh.androidx.lite.utils.Constant;
import com.pengxh.androidx.lite.utils.LoadState;
import com.pengxh.androidx.lite.utils.LoadingDialog;

import java.util.ArrayList;

public class SanBaoHandledDetailActivity extends AndroidxBaseActivity<ActivitySanBaoHandledBinding> {

    private final Context context = SanBaoHandledDetailActivity.this;
    private CaseViewModel caseViewModel;
    private CaseDetailBean.DataBean data = new CaseDetailBean.DataBean();

    @Override
    protected void setupTopBarLayout() {
        ViewGroupKit.initImmersionBar(binding.rootView, this, true, R.color.white);

        binding.titleBarLayout.titleView.setText("案卷详情");
        binding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        String id = getIntent().getStringExtra(Constant.INTENT_PARAM_KEY);
        caseViewModel = new ViewModelProvider(this).get(CaseViewModel.class);
        caseViewModel.getCaseDetail(this, id);
    }

    @Override
    protected void initEvent() {
        caseViewModel.caseDetailModel.observe(this, new Observer<CaseDetailBean>() {
            @Override
            public void onChanged(CaseDetailBean resultBean) {
                if (resultBean.getCode() == 200) {
                    data = resultBean.getData().get(0);
                    binding.caseNumberView.setText(data.getCaseid());
                    binding.caseHandleStatusView.setText(data.getCaseStateName());
                    binding.caseTypeView.setText(data.getEorcName());
                    binding.caseLargeTypeView.setText(data.getCasetypeName());
                    binding.caseSmallTypeView.setText(data.getCasetypeDetailName());
                    binding.caseCommunityView.setText(data.getCommunityName().equals("") ? "无更详细社区信息" : data.getCommunityName());
                    binding.casePlaceView.setText(data.getFieldintro());
                    binding.caseSituationView.setText(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));
                            }
                        }
                        ReadOnlyImageAdapter imageAdapter = new ReadOnlyImageAdapter(context, urls);
                        binding.imageGridView.setAdapter(imageAdapter);
                        //九宫格点击事件
                        binding.imageGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                                if (urls.get(position).isEmpty()) {
                                    StringKit.show(context, "图片加载失败，无法查看大图");
                                } else {
                                    ContextKit.navigatePageTo(context, BigImageActivity.class, 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));
                            }
                        }
                        ReadOnlyImageAdapter imageAdapter = new ReadOnlyImageAdapter(context, urls);
                        binding.handleGridView.setAdapter(imageAdapter);
                        //九宫格点击事件
                        binding.handleGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                                if (urls.get(position).isEmpty()) {
                                    StringKit.show(context, "图片加载失败，无法查看大图");
                                } else {
                                    ContextKit.navigatePageTo(context, BigImageActivity.class, position, urls);
                                }
                            }
                        });
                    }

                    //处罚记录
                    binding.punishTypeView.setText(StringHelper.TypeToString(data.getPunishType()));
                    binding.punishOpinionView.setText(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));
                            }
                        }
                        ReadOnlyImageAdapter imageAdapter = new ReadOnlyImageAdapter(context, urls);
                        binding.punishGridView.setAdapter(imageAdapter);
                        //九宫格点击事件
                        binding.punishGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                                if (urls.get(position).isEmpty()) {
                                    StringKit.show(context, "图片加载失败，无法查看大图");
                                } else {
                                    ContextKit.navigatePageTo(context, BigImageActivity.class, position, urls);
                                }
                            }
                        });
                    }
                    binding.punishDateView.setText(data.getPunishTime());
                }
            }
        });
        caseViewModel.loadState.observe(this, new Observer<LoadState>() {
            @Override
            public void onChanged(LoadState loadState) {
                if (loadState == LoadState.Loading) {
                    LoadingDialog.show(SanBaoHandledDetailActivity.this, "获取数据中...");
                } else {
                    LoadingDialog.dismiss();
                }
            }
        });

        binding.locationMapView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MapHelper.displayOnMap(context, data.getEorcName(), data.getLng(), data.getLat());
            }
        });

        binding.handleLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContextKit.navigatePageTo(context, CaseProcessActivity.class, data.getProcessId());
            }
        });
    }
}