package com.sjkj.appthreefloor_tsgz.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.tabs.TabLayout;
import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.activity.viewModel.ResourceVisualizationViewModel;
import com.sjkj.appthreefloor_tsgz.app.App;
import com.sjkj.appthreefloor_tsgz.bean.ResourceVisualizationBean;
import com.sjkj.appthreefloor_tsgz.databinding.ActivityResourceWaterCraneBinding;
import com.sjkj.appthreefloor_tsgz.fragment.CommonDataListFragment;
import com.sjkj.appthreefloor_tsgz.fragment.CommonMapFragment;
import com.sjkj.appthreefloor_tsgz.map.MapMarkBean;
import com.sjkj.appthreefloor_tsgz.net.NewDataCenterRequest;
import com.sjkj.base_lib.entity.Params;
import com.sjkj.base_lib.net.domain.interactor.ProcessErrorSubscriber;
import com.sjkj.base_lib.utils.Constants;
import com.sjkj.base_lib.utils.ToastUtils;
import com.sjkj.base_lib.view.BaseActivity;
import com.sjkj.base_lib.weight.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

public class WaterCraneResourcesActivity extends BaseActivity {
    private ResourceVisualizationViewModel viewModel;
    private ActivityResourceWaterCraneBinding binding;
   // private ViewPagerAdapter pagerAdapter;
    private NewDataCenterRequest newDataCenterRequest = new NewDataCenterRequest(App.getInstance());
    private List<ResourceVisualizationBean> mList = new ArrayList<>();
    private List<MapMarkBean> mapList = new ArrayList<>();
    private String eventTypeName = "肉牛";
    CommonDataListFragment dataListFragment;
    CommonMapFragment mapFragment;
    private FragmentTransaction ft;
    private FragmentManager fm;

    @Override
    protected int initLayout() {
        return R.layout.activity_resource_water_crane;
    }

    @Override
    public void initBinding() {
        binding = (ActivityResourceWaterCraneBinding) getViewDataBinding();
        viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(ResourceVisualizationViewModel.class);
        binding.setViewModel(viewModel);
        binding.setLifecycleOwner(this);
    }

    @Override
    public void initView() {
        super.initView();
        //eventTypeName = getIntent().getStringExtra("name");
        getTitleView().setText(getIntent().getStringExtra("title"));
        binding.tabLayout.addTab(binding.tabLayout.newTab().setText("数据"));
        binding.tabLayout.addTab(binding.tabLayout.newTab().setText("地图"));
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (TextUtils.equals(tab.getText(), "数据")) {
                    getSupportFragmentManager().beginTransaction()
                            .show(dataListFragment)
                            .hide(mapFragment)
                            .commit();
                } else {
                    getSupportFragmentManager().beginTransaction()
                            .hide(dataListFragment)
                            .show(mapFragment)
                            .commit();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        dataListFragment = new CommonDataListFragment(eventTypeName);
        mapFragment = new CommonMapFragment(eventTypeName);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_layout, dataListFragment)
                .add(R.id.fragment_layout, mapFragment)
                .show(dataListFragment)
                .hide(mapFragment)
                .commit();
    }


    @Override
    public void initListener() {
        super.initListener();

        binding.searchTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.show("搜索");
            }
        });
    }


    private void requestListRefresh() {
        mList.clear();
        Params params = new Params();

        params.put("longitude", getIntent().getDoubleExtra("Longitude", 0.0));
        params.put("latitude", getIntent().getDoubleExtra("Latitude", 0.0));
        params.put("type", "centerdata_t_farm_filings_livestock");
        params.put("assort", "app");
//        if (!TextUtils.isEmpty(getIntent().getStringExtra("radius"))) {
//            params.put("radius", getIntent().getStringExtra("radius"));
//        }
//        if (getIntent().getStringExtra("assort").equals("leader")) {
//            params.put("deptId", AppTools.getDeptId());
//        }

        newDataCenterRequest.setParams(params);
        newDataCenterRequest.execute(new ProcessErrorSubscriber<List<ResourceVisualizationBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                hideLoadingDialog(LoadingDialog.LOADING_FAILED);
                ToastUtils.show(e.getMessage());
            }

            @Override
            public void onNext(List<ResourceVisualizationBean> list) {
                super.onNext(list);
                hideLoadingDialog(LoadingDialog.LOADING_SUCCESS);
                //列表页填充数据
                mList.addAll(list);
                dataListFragment.updataUI(mList, eventTypeName);
                //地图页填充数据
                MapMarkBean bean;
                if (mList != null && mList.size() > 0) {
                    for (int i = 0; i < mList.size(); i++) {
                        bean = new MapMarkBean();
                        bean.setLatitude(Double.parseDouble(mList.get(i).getLatitude()));
                        bean.setLongitude(Double.parseDouble(mList.get(i).getLongitude()));
                        bean.setName(mList.get(i).getName());
                        bean.setId(mList.get(i).getId());
                        mapList.add(bean);
                    }
                    mapFragment.refreshData(mList);
                }
//                } else {
//                    bean = new MapMarkBean();
//                    bean.setLatitude(getIntent().getDoubleExtra("Latitude", Constants.DEFAULT_LOCATION_LAT));
//                    bean.setLongitude(getIntent().getDoubleExtra("Longitude", Constants.DEFAULT_LOCATION_LNG));
//                    bean.setId("0");
//                    bean.setName("附近暂无资源");
//                    mapList.add(bean);
//                    mapFragment.refreshData(mapList, mList, eventTypeName, 1);
//                }
                //finishRefreshOrLoadMore(binding.smartRefreshLayout);
            }
        });
    }

    @Override
    public void loadData() {
        super.loadData();
        showLoadingDialog();
        requestListRefresh();
    }
}
