package com.casic.dcms.view.statistics;

import android.content.Context;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.casic.dcms.R;
import com.casic.dcms.databinding.ActivityCityBaseDataBinding;
import com.casic.dcms.model.CityBaseDataBean;
import com.casic.dcms.utils.DialogHelper;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.vm.DataAnalysisViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.pengxh.androidx.lite.adapter.NormalRecyclerAdapter;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.hub.IntHub;
import com.pengxh.androidx.lite.utils.ImmerseStatusBarManager;
import com.pengxh.androidx.lite.vm.LoadState;

/**
 * @author : Pengxh
 * @time : 2021/4/16 9:24
 * @email : 290677893@qq.com
 */
public class CityBaseDataActivity extends AndroidxBaseActivity<ActivityCityBaseDataBinding> {

    private static final String TAG = "CityBaseDataActivity";
    private final Context context = CityBaseDataActivity.this;
    private DataAnalysisViewModel dataAnalysisViewModel;

    @Override
    protected void setupTopBarLayout() {
        ImmerseStatusBarManager.setColor(this, IntHub.convertColor(this, R.color.white));
        ImmersionBar.with(this).statusBarDarkFont(true).init();

        viewBinding.titleBarLayout.titleView.setText(LocaleConstant.ANALYSIS_ITEMS.get(0));
        viewBinding.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) {
                    DialogHelper.showLoadingDialog(context, "数据加载中，请稍后");
                } else {
                    DialogHelper.dismissLoadingDialog();
                }
            }
        });
    }

    @Override
    protected void initData() {
        dataAnalysisViewModel = new ViewModelProvider(this).get(DataAnalysisViewModel.class);
        dataAnalysisViewModel.getCityBaseData();
        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());
                    }
                };
                viewBinding.baseDataRecyclerView.setAdapter(adapter);
            }
        });
    }

    @Override
    protected void initEvent() {

    }
}