package com.sjkj.appthreefloor_tsgz.fragment;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.kongzue.dialogx.dialogs.MessageDialog;
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;
import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.adapter.EventDetailsAdapter;
import com.sjkj.appthreefloor_tsgz.app.App;
import com.sjkj.appthreefloor_tsgz.bean.EventDetailsBean;
import com.sjkj.appthreefloor_tsgz.bean.MessageEvent;
import com.sjkj.appthreefloor_tsgz.databinding.FragmentEventDetailsBinding;
import com.sjkj.appthreefloor_tsgz.fragment.viewModel.EventFargmentViewModel;
import com.sjkj.appthreefloor_tsgz.net.EventChooseStatusRequest;
import com.sjkj.appthreefloor_tsgz.net.EventDetailsRequest;
import com.sjkj.base_lib.entity.Params;
import com.sjkj.base_lib.net.domain.interactor.ProcessErrorSubscriber;
import com.sjkj.base_lib.utils.LocationUtils;
import com.sjkj.base_lib.utils.ToastUtils;
import com.sjkj.base_lib.view.BaseFragment;
import com.sjkj.base_lib.weight.LoadingDialog;

public class EventDetailsFragment extends BaseFragment {
    private FragmentEventDetailsBinding binding;
    private EventFargmentViewModel viewModel;
    private EventDetailsAdapter adapter;
    EventDetailsRequest eventDetailsRequest = new EventDetailsRequest(App.getInstance());
    EventChooseStatusRequest chooseStatusRequest = new EventChooseStatusRequest(App.getInstance());

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

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

    @Override
    public void initView() {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(RecyclerView.VERTICAL);
        binding.recyclerView.setLayoutManager(manager);
        adapter = new EventDetailsAdapter(getActivity());
        binding.recyclerView.setAdapter(adapter);
        viewModel.eventId.set(getActivity().getIntent().getStringExtra("id"));


    }

    @Override
    public void initListener() {

        binding.tvCompleted.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChangeStatus("30", binding.tvCompleted.getText().toString());
            }
        });
        binding.tvFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChangeStatus("50", binding.tvFinish.getText().toString());
            }
        });
        binding.tvRefuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChangeStatus("40", binding.tvRefuse.getText().toString());
            }
        });
        binding.mapLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewModel.longitude.get() != 0 && viewModel.latitude.get() != 0) {
//                    if (viewModel.markListBean.get() != null) {
//                        MapActivity.open(EventDetailsActivity.this, viewModel.markListBean.get());
//                    }
                    LocationUtils.getInstance().mapNav(viewModel.latitude.get(), viewModel.longitude.get(), "");
                }

            }
        });
    }

    private void ChangeStatus(String status, String str) {
        MessageDialog.show("温馨提示", "是否确定" + str + "?", "确定", "取消")
                .setCancelButton(new OnDialogButtonClickListener<MessageDialog>() {
                    @Override
                    public boolean onClick(MessageDialog baseDialog, View v) {
                        baseDialog.dismiss();
                        return false;
                    }
                })
                .setOkButton(new OnDialogButtonClickListener<MessageDialog>() {
                    @Override
                    public boolean onClick(MessageDialog baseDialog, View v) {
                        showLoadingDialog();
                        Params params = new Params();
                        params.put("eventId", viewModel.eventId.get());
                        params.put("id", getActivity().getIntent().getStringExtra("eventCode"));
                        params.put("status", status);
                        chooseStatusRequest.setParams(params);
                        chooseStatusRequest.execute(new ProcessErrorSubscriber() {
                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                hideLoadingDialog(LoadingDialog.LOADING_FAILED);
                                if (e != null) {
                                    ToastUtils.show(e.getMessage());
                                }
                            }

                            @Override
                            public void onNext(Object o) {
                                super.onNext(o);
                                hideLoadingDialog(LoadingDialog.LOADING_SUCCESS);
                                ToastUtils.show("操作成功");
                                getActivity().setResult(1000);
                                getActivity().finish();
                            }
                        });

                        return false;
                    }
                });


    }

    @Override
    public void loadData() {
        showLoadingDialog();
        Params params = new Params();
        params.put("id", viewModel.eventId.get());

        eventDetailsRequest.setParams(params);
        eventDetailsRequest.execute(new ProcessErrorSubscriber<EventDetailsBean>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                hideLoadingDialog(LoadingDialog.LOADING_FAILED);

            }

            @Override
            public void onNext(EventDetailsBean bean) {
                super.onNext(bean);
                hideLoadingDialog(LoadingDialog.LOADING_SUCCESS);
                if (bean != null && bean.getDepts().size() > 0) {
                    viewModel.title.set(bean.getEventName());
                    viewModel.time.set(bean.getCreateTime());
                    viewModel.remark.set(bean.getRemark());
                    viewModel.longitude.set(Double.parseDouble(bean.getLongitude()));
                    viewModel.latitude.set(Double.parseDouble(bean.getLatitude()));
                    MessageEvent event = new MessageEvent();
                    event.setEventType(bean.getEventType());
                    event.setEventName(bean.getEventName());
                    event.setLatitude(bean.getLatitude());
                    event.setLongitude(bean.getLongitude());
                    viewModel.setMessage(event);
                    adapter.refreshList(bean.getDepts());
                    if (bean.getStatus().equals("20")) {
                        //待签收
                        binding.tvCompleted.setVisibility(View.VISIBLE);
                        binding.tvRefuse.setVisibility(View.VISIBLE);
                        binding.tvTime.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
                    } else if (bean.getStatus().equals("30")) {
                        //处理中
                        binding.tvFinish.setVisibility(View.VISIBLE);
                        binding.tvTime.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
                    } else {
                        binding.tvCompleted.setVisibility(View.GONE);
                        binding.tvRefuse.setVisibility(View.GONE);
                        binding.tvFinish.setVisibility(View.GONE);
                        binding.tvTime.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
                    }
                }
            }
        });
    }
}
