package com.sjkj.appthreefloor_tsgz.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.adapter.CameraAdapter;
import com.sjkj.appthreefloor_tsgz.adapter.RightAdapter;
import com.sjkj.appthreefloor_tsgz.app.App;
import com.sjkj.appthreefloor_tsgz.bean.RightModelBean;
import com.sjkj.appthreefloor_tsgz.databinding.ActivityCameraListBinding;
import com.sjkj.appthreefloor_tsgz.net.CameraAllListRequest;
import com.sjkj.base_lib.adapter.BaseRecAdapter;
import com.sjkj.base_lib.camera.dh.bean.CameraBean;
import com.sjkj.base_lib.camera.dh.view.DHCameraDetailsActivity;
import com.sjkj.base_lib.camera.dh.vm.CameraViewModel;
import com.sjkj.base_lib.divider.DividerItem;
import com.sjkj.base_lib.entity.Params;
import com.sjkj.base_lib.net.domain.interactor.ProcessErrorSubscriber;
import com.sjkj.base_lib.utils.AppTools;
import com.sjkj.base_lib.view.BaseActivity;

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

/**
 * 监控中心
 */
public class MonitoringCenterActivity extends BaseActivity {

    public static final String TAG = MonitoringCenterActivity.class.getSimpleName();
    private CameraAdapter adapter;
    private RightAdapter rightAdapter;
    //    private TypeAdapter typeAdapter;
    private List<RightModelBean> typeList = new ArrayList<>();
    //    private List<RegionModel> regionList = new ArrayList<>();
    private List<CameraBean> list = new ArrayList<>();
    private int currentPage = 1;
    private ActivityCameraListBinding binding;
    private CameraViewModel viewModel;
    private CameraAllListRequest cameraListRequest = new CameraAllListRequest(App.getInstance());
    private String titleName;

    @Override
    public int initLayout() {
        return R.layout.activity_camera_list;
    }

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

    @Override
    public void initView() {
        Drawable drawable = getDrawable(R.drawable.icon_camera);
        drawable.setBounds(0, 30, 60, 90);
        getRightView().setCompoundDrawables(null, drawable, null, null);
        getRightView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (binding.drawerLayout.isDrawerVisible(binding.rightLayout)) {
                    binding.drawerLayout.closeDrawers();
                } else {
                    binding.drawerLayout.openDrawer(binding.rightLayout);
                }
            }
        });
        getRightView().setVisibility(View.VISIBLE);
        titleName = getIntent().getStringExtra("titleName");
        //DrawerAdapter adapter1 = new DrawerAdapter(items);

        FlexboxLayoutManager manager = new FlexboxLayoutManager();
//        //设置主轴排列方式
        manager.setFlexDirection(FlexDirection.ROW);
        //设置是否换行
        manager.setFlexWrap(FlexWrap.WRAP);
        manager.setAlignItems(AlignItems.BASELINE);
        binding.recyclerView.setLayoutManager(manager);

        FlexboxLayoutManager manager1 = new FlexboxLayoutManager();
        //设置主轴排列方式
        manager1.setFlexDirection(FlexDirection.ROW);
        //设置是否换行
        manager1.setFlexWrap(FlexWrap.WRAP);
        manager1.setAlignItems(AlignItems.BASELINE);
        binding.typerecyclerView.setLayoutManager(manager1);

        rightAdapter = new RightAdapter(this);
        binding.typerecyclerView.setAdapter(rightAdapter);

//        if (TextUtils.isEmpty(titleName)) {
//            getTitleView().setText(getString(R.string.monitor_center));
//        } else {
        getTitleView().setText(titleName);
        // }
//        Drawable drawable = getDrawable(R.drawable.event_search_img);
//        drawable.setBounds(0, 30, 60, 90);
//        getRightView().setCompoundDrawables(null, drawable, null, null);
//        getRightView().setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (binding.drawerLayout.isDrawerVisible(binding.rightLayout)) {
//                    binding.drawerLayout.closeDrawers();
//                } else {
//                    binding.drawerLayout.openDrawer(binding.rightLayout);
//                }
//            }
//        });
//        getRightView().setVisibility(View.GONE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        binding.mainRecyclerView.setLayoutManager(linearLayoutManager);
        binding.mainRecyclerView.addItemDecoration(new DividerItem(this));

        adapter = new CameraAdapter(this);
        binding.mainRecyclerView.setAdapter(adapter);

        binding.smartRefreshLayout.setEnableRefresh(true);
        binding.smartRefreshLayout.setEnableLoadMore(true);

//
    }

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

    @Override
    public void initListener() {
        binding.searchTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestListRefresh();
            }
        });
        binding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                requestListRefresh();
            }
        });
        binding.smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                requestListLoadMore();
            }
        });
//        regionAdapter.setListener(item -> {
////            deptId = item;
//        });
//
//        typeAdapter.setListener(item -> {
////            typeModel = item;
//        });
        adapter.setItemClick(new BaseRecAdapter.OnItemClick<CameraBean>() {
            @Override
            public void onItemClick(CameraBean bean, int position) {
                if (AppTools.getCameraType() == AppTools.CAMERA_TYPE_NONE) {

                } else if (AppTools.getCameraType() == AppTools.CAMERA_TYPE_DH) {
                    Intent intent = new Intent(MonitoringCenterActivity.this, DHCameraDetailsActivity.class);
                    intent.putExtra("cameraId", bean.getId());
                    startActivity(intent);
                } else if (AppTools.getCameraType() == AppTools.CAMERA_TYPE_HK) {
//                    Intent intent = new Intent(MonitoringCenterActivity.this, HKH5CameraActivity.class);
//                    intent.putExtra("cameraId", bean.getId());
//                    startActivity(intent);
                }
//                intent.putExtra("deviceName", bean.getCameraId());
//                intent.putExtra("deviceIp", bean.getCameraId());
//                intent.putExtra("deviceUserName", bean.getCameraId());
//                intent.putExtra("devicePassword", bean.getCameraId());
//                intent.putExtra("devicePort", bean.getCameraId());
            }
        });
    }

    @Override
    public void loadData() {
        requestListRefresh();
//        LocationUtils.getInstance().startLocation(true, new LocationUtils.OnLocationListener() {
//            @Override
//            public void onLocationChanged(double lat,double lng,String address,int code) {
////                ToastUtils.show(aMapLocation.getAddress());
//                viewModel.latitude.set(aMapLocation.getLatitude());
//                viewModel.longitude.set(aMapLocation.getLongitude());
//                cameraList();
//            }
//        });

        typeList.add(new RightModelBean("camera_type_2", "森林防火", false));
        typeList.add(new RightModelBean("camera_type_4", "病虫害", false));
        typeList.add(new RightModelBean("camera_type_1", "秸秆禁烧", false));
        typeList.add(new RightModelBean("camera_type_3", "粪污处理", false));
        typeList.add(new RightModelBean("camera_type_5", "水利监控", false));
        typeList.add(new RightModelBean("camera_type_6", "违法建筑", false));
        typeList.add(new RightModelBean("camera_type_7", "火情防控", false));
        rightAdapter.setList(typeList);
    }

    private void requestListRefresh() {
        currentPage = 1;
        Params params = new Params();
        params.put("cameraName", viewModel.keyword.get());
        params.put("pageNum", currentPage);
        params.put("deptId", AppTools.getDeptId());
        if (titleName.equals("数字公安")) {
            params.put("type", "11");
        } else {
            params.put("type", "1,2,3,4,5,6,7,8,9,10,11,12");
        }
        params.put("pageSize", 20);
        cameraListRequest.setParams(params);
        cameraListRequest.execute(new ProcessErrorSubscriber<List<CameraBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                finishRefreshOrLoadMore(binding.smartRefreshLayout);
            }

            @Override
            public void onNext(List<CameraBean> beanList) {
                super.onNext(beanList);
                list.clear();
                if (beanList != null && beanList.size() > 0) {
                    list.addAll(beanList);
                }
                adapter.refreshList(list);
                finishRefreshOrLoadMore(binding.smartRefreshLayout);
            }
        });
    }

    private void requestListLoadMore() {
        Params params = new Params();
        params.put("cameraName", viewModel.keyword.get());
        params.put("pageSize", 20);
        params.put("deptId", AppTools.getDeptId());
        if (titleName.equals("数字公安")) {
            params.put("type", "11");
        } else {
            params.put("type", "1,2,3,4,5,6,7,8,9,10,11,12");
        }
        params.put("pageNum", currentPage + 1);
        cameraListRequest.setParams(params);
        cameraListRequest.execute(new ProcessErrorSubscriber<List<CameraBean>>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                finishRefreshOrLoadMore(binding.smartRefreshLayout);
            }

            @Override
            public void onNext(List<CameraBean> beanList) {
                super.onNext(beanList);
                if (beanList != null && beanList.size() > 0) {
                    currentPage = currentPage + 1;
                    list.addAll(beanList);
                    adapter.refreshList(list);
                }
                finishRefreshOrLoadMore(binding.smartRefreshLayout);
            }
        });
    }
}
