package com.sjkj.appthreefloor_tsgz.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.lifecycle.ViewModelProvider;

import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.activity.ExoPlayerActivity;
import com.sjkj.appthreefloor_tsgz.activity.MonitoringCenterActivity;
import com.sjkj.appthreefloor_tsgz.activity.MyVideoPlayerActivity;
import com.sjkj.appthreefloor_tsgz.activity.WaterCraneResourcesActivity;
import com.sjkj.appthreefloor_tsgz.app.App;
import com.sjkj.appthreefloor_tsgz.bean.NoticeListBean;
import com.sjkj.appthreefloor_tsgz.databinding.FragmentMainBinding;
import com.sjkj.appthreefloor_tsgz.net.HomeNoticeListRequest;
import com.sjkj.base_lib.entity.Params;
import com.sjkj.base_lib.net.domain.interactor.ProcessErrorSubscriber;
import com.sjkj.base_lib.utils.CommonUtils;
import com.sjkj.base_lib.utils.LocationUtils;
import com.sjkj.base_lib.utils.ToastUtils;
import com.sjkj.base_lib.view.BaseFragment;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

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

public class MainFragment extends BaseFragment {
    private FragmentMainBinding binding;
    private MainViewModel viewModel;
    //private BannerRequest bannerRequest = new BannerRequest(App.getInstance());
    private HomeNoticeListRequest homeNoticeListRequest = new HomeNoticeListRequest(App.getInstance());

    private List<String> banners;

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

    @Override
    public View initBinding(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        binding = (FragmentMainBinding) getViewDataBing(inflater, container);
        viewModel = new ViewModelProvider(getActivity(), new ViewModelProvider.NewInstanceFactory()).get(MainViewModel.class);
        binding.setViewModel(viewModel);
        binding.setLifecycleOwner(this);
        return binding.getRoot();
    }

    @Override
    public void initView() {
    }

    @Override
    public void initListener() {
        binding.llCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), MonitoringCenterActivity.class).putExtra("titleName", getString(R.string.monitor_center)));
            }
        });
        //传感器
        binding.llSensor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(getActivity(), ExoPlayerActivity.class));
            }
        });
        //水鹤
        binding.llWaterCrane.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                LocationUtils.getInstance().startLocation(true, new LocationUtils.OnLocationListener() {
                    @Override
                    public void onLocationChanged(double lat, double lng, String address, int code) {


                        if (CommonUtils.checkLongitudeLatitude(lng + "", lat + "")) {
                            // hideLoadingDialog(LoadingDialog.LOADING_SUCCESS);
                            startActivity(new Intent(getActivity(), WaterCraneResourcesActivity.class).putExtra("Longitude", lng).putExtra("Latitude", lat));
                        } else {
                            ToastUtils.show("定位失败，请检查定位权限或者稍后重试");
                        }

                    }
                });
            }
        });
        //取水口
        binding.llWaterIntake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(getActivity(), MyVideoPlayerActivity.class).putExtra("videoUrl", "https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.ism/.m3u8"));
            }
        });
        //水源
        binding.llWaterSource.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    @Override
    public void loadData() {
        getNoticelist();
        ArrayList imgs = new ArrayList();
        imgs.add(R.drawable.icon_banner1);
        imgs.add(R.drawable.icon_banner2);

        binding.banner.setLoopTime(1500);
        binding.banner.setIndicator(new CircleIndicator(getActivity()));
        //binding.banner.setBannerGalleryEffect(50, 10);
        binding.banner.setAdapter(new BannerImageAdapter<Integer>(imgs) {
            @Override
            public void onBindView(BannerImageHolder holder, Integer data, int position, int size) {
                holder.imageView.setImageResource((Integer) imgs.get(position));
            }
        }).start();

//        bannerRequest.setParams(new Params());
//        bannerRequest.execute(new ProcessErrorSubscriber<BannerDataBean>() {
//            @Override
//            public void onError(Throwable e) {
//                super.onError(e);
//            }
//
//            @Override
//            public void onNext(BannerDataBean bannerDataBean) {
//                super.onNext(bannerDataBean);
//                if (bannerDataBean != null) {
//                    List<BannerDataBean.ListBean> list = bannerDataBean.getList();
//                    List<String> imgs = new ArrayList<>();
//                    if (list != null && list.size() > 0) {
//                        for (int i = 0; i < list.size(); i++) {
//                            if (list.get(i) != null && !TextUtils.isEmpty(list.get(i).getPath())) {
//                                imgs.add(list.get(i).getPath());
//                            }
//                        }
//                        if (imgs.size() > 0) {
//                            imgs.add("https://photo.tuchong.com/250829/f/31548923.jpg");
//                            imgs.add("https://photo.tuchong.com/46728/f/20138526.jpg");
//                            imgs.add("https://photo.tuchong.com/392724/f/16858773.jpg");
//                            imgs.add("https://photo.tuchong.com/408963/f/18401047.jpg");
//
//                            binding.banner.setAdapter(new BannerImageAdapter<String>(imgs) {
//                                @Override
//                                public void onBindView(BannerImageHolder holder, String data, int position, int size) {
//                                    //图片加载自己实现
//                                    Glide.with(holder.itemView)
//                                            .load(data)
//                                            .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
//                                            .into(holder.imageView);
//                                }
//                            });
//                            binding.banner.setLoopTime(1500);
//                            binding.banner.setIndicator(new CircleIndicator(getActivity()));
//                            //binding.banner.setIndicator(new CircleIndicator(getActivity()));
//                            //添加画廊效果
//                            //binding.banner.setBannerGalleryEffect(50, 10);
//                            //binding.banner.setBannerGalleryMZ(20);
//                        }
//                    }
//
//                }
//            }
//        });
    }

    private void getNoticelist() {
        homeNoticeListRequest.setParams(new Params());
        homeNoticeListRequest.execute(new ProcessErrorSubscriber<List<NoticeListBean>>() {

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }

            @Override
            public void onNext(List<NoticeListBean> noticelist) {
                super.onNext(noticelist);
                if (noticelist != null && noticelist.size() > 0) {
                    for (int i = 0; i < noticelist.size(); i++) {
                        if (i == 0) {
                            binding.tvOne.setText(noticelist.get(i).getNoticeTitle());
                        } else if (i == 1) {
                            binding.tvTwo.setText(noticelist.get(i).getNoticeTitle());
                        }
                    }
                }

            }
        });

    }
}



