package com.zztrapp.zztrapp.ui.currency.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.bean.WrwxcBean;
import com.zztrapp.zztrapp.constant.Constant;
import com.zztrapp.zztrapp.databinding.ActivityWrwxcBinding;
import com.zztrapp.zztrapp.http.HttpMethods;
import com.zztrapp.zztrapp.http.ISubscriberOnNextListener;
import com.zztrapp.zztrapp.http.ProgressSubscriber;
import com.zztrapp.zztrapp.http.RxApiManager;
import com.zztrapp.zztrapp.ui.currency.adapter.WrwxcAdapter;
import com.zztrapp.zztrapp.utilcode.mUtil.MyUtil;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;

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

/**
 * Describe 污染物巡检-分页列表查询
 * Created by Bby on 2022/8/3
 * Email bby0856@163.com
 */
public class WrwxcActivity extends BaseActivity {

    private ActivityWrwxcBinding binding;
    private int pageNo = 1;//页数
    private int total = 0;//总共多少页
    private String searchName = "";//搜索值
    private boolean isShowDialog = true;
    private boolean isRefresh = true;//true 刷新 false 加载
    private List<WrwxcBean.RecordsBean> list = new ArrayList<>();
    private WrwxcAdapter adapter;

    @Override
    public View getLayoutId() {
        return binding.getRoot();
    }

    @Override
    protected void initBinding() {
        binding = ActivityWrwxcBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onResume() {
        super.onResume();
        isShowDialog = false;
        isRefresh = true;
        pageNo = 1;
        loadList();
    }

    @Override
    protected void init() {
        binding.navView.navTitle.setText("污染物巡检");
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.refresh.setRefreshHeader(new ClassicsHeader(WrwxcActivity.this));
        binding.refresh.setRefreshFooter(new ClassicsFooter(WrwxcActivity.this));
        binding.refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isShowDialog = false;
                isRefresh = false;
                pageNo += 1;
                if (pageNo <= total) {
                    loadList();
                }
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isShowDialog = false;
                isRefresh = true;
                pageNo = 1;
                loadList();
                refreshLayout.finishRefresh();
            }
        });

        binding.rv.setLayoutManager(new LinearLayoutManager(WrwxcActivity.this, LinearLayoutManager.VERTICAL, false));
        adapter = new WrwxcAdapter(WrwxcActivity.this);
        binding.rv.setAdapter(adapter);
        binding.searchEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                searchName = charSequence.toString();
                if (charSequence.length() > 0) {
                    binding.del.setVisibility(View.VISIBLE);
                } else {
                    binding.del.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    @Override
    protected void initOnClick() {
        binding.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(WrwxcActivity.this, AddWrwxcActivity.class);
                intent.putExtra("jumpType", 0);
                intent.putExtra("navName", "添加添加巡检信息");
                startActivity(intent);
            }
        });
        adapter.setOnItemClickListener(new WrwxcAdapter.OnItemClickListener() {
            @Override
            public void onEditClick(String id, int position) {
                Intent intent = new Intent(new Intent(WrwxcActivity.this, AddWrwxcActivity.class));
                intent.putExtra("jumpType", 1);
                intent.putExtra("detail", list.get(position));
                intent.putExtra("navName", "编辑巡检信息");
                startActivity(intent);
            }

            @Override
            public void onLookDetailClick(String id, int position) {
                Intent intent = new Intent(new Intent(WrwxcActivity.this, AddWrwxcActivity.class));
                intent.putExtra("jumpType", 2);
                intent.putExtra("detail", list.get(position));
                intent.putExtra("navName", "巡检信息详情");
                startActivity(intent);
            }
        });
        binding.search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isShowDialog = true;
                isRefresh = true;
                pageNo = 1;
                loadList();
            }
        });
        binding.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchName = "";
                binding.searchEd.setText(searchName);
                binding.del.setVisibility(View.GONE);
            }
        });
        binding.searchEd.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
                    searchName = arg0.getText().toString().trim();
                    isShowDialog = true;
                    isRefresh = true;
                    pageNo = 1;
                    loadList();
                }
                return false;
            }
        });
    }

    //列表
    private void loadList() {
        ISubscriberOnNextListener<HttpResult<WrwxcBean>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<WrwxcBean>>() {
            @Override
            public void onNext(HttpResult<WrwxcBean> httpResult) {
                if (httpResult.getCode() == 200) {
                    total = httpResult.getResult().getPages();
                    if (isRefresh) {
                        list.clear();
                    }
                    list.addAll(httpResult.getResult().getRecords());
                    for (int i = 0; i < list.size(); i++) {
                        if (!TextUtils.isEmpty(list.get(i).getJd())) {
                            list.get(i).setJd(MyUtil.getNumberPoint(Double.parseDouble(list.get(i).getJd())));
                        }
                        if (!TextUtils.isEmpty(list.get(i).getWd())) {
                            list.get(i).setWd(MyUtil.getNumberPoint(Double.parseDouble(list.get(i).getWd())));
                        }
                    }
                    adapter.setData(list);
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<WrwxcBean>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, this, isShowDialog);
        RxApiManager.get().add("wrwxjPagelist", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).wrwxjPagelist(httpResultProgressSubscriber, pageNo, Constant.PAGE_SIZE, searchName);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
        RxApiManager.get().cancel("wrwxjPagelist");
    }
}