package com.casic.dcms.view.statistics;

import android.os.Bundle;

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

import com.casic.dcms.R;
import com.casic.dcms.adapter.NormalRecyclerAdapter;
import com.casic.dcms.databinding.ActivityCityBaseDataBinding;
import com.casic.dcms.model.CityBaseDataBean;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.ViewGroupKit;
import com.casic.dcms.vm.DataAnalysisViewModel;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.utils.LoadState;
import com.pengxh.androidx.lite.utils.LoadingDialog;

public class CityBaseDataActivity extends AndroidxBaseActivity<ActivityCityBaseDataBinding> {

    private static final String TAG = "CityBaseDataActivity";
    private DataAnalysisViewModel dataAnalysisViewModel;

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

        binding.titleBarLayout.titleView.setText(LocaleConstant.ANALYSIS_ITEMS.get(0));
        binding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

    @Override
    protected void observeRequestState() {
        dataAnalysisViewModel.loadState.observe(this, new Observer<LoadState>() {
            @Override
            public void onChanged(LoadState loadState) {
                if (loadState == LoadState.Loading) {
                    LoadingDialog.show(CityBaseDataActivity.this, "数据加载中，请稍后");
                } else {
                    LoadingDialog.dismiss();
                }
            }
        });
    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        dataAnalysisViewModel = new ViewModelProvider(this).get(DataAnalysisViewModel.class);
        dataAnalysisViewModel.getCityBaseData(this);
        dataAnalysisViewModel.cityBaseData.observe(this, new Observer<CityBaseDataBean>() {
            @Override
            public void onChanged(CityBaseDataBean cityBaseDataBean) {
                NormalRecyclerAdapter<CityBaseDataBean.DataModel> adapter = new NormalRecyclerAdapter<CityBaseDataBean.DataModel>(R.layout.item_base_city_data_rv_g, cityBaseDataBean.getData()) {

                    @Override
                    public void convertView(ViewHolder viewHolder, int position, CityBaseDataBean.DataModel item) {
                        viewHolder.setImageResource(R.id.dataIconView, StringHelper.obtainIcon(item.getDcmName()))
                                .setText(R.id.dataTitleView, item.getDcmName())
                                .setText(R.id.dataValueView, item.getVal());
                    }
                };
                binding.baseDataRecyclerView.setAdapter(adapter);
            }
        });
    }

    @Override
    protected void initEvent() {

    }
}