package com.zztrapp.zztrapp.ui.index.activity;

import android.content.Intent;
import android.text.Editable;
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.R;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.bean.HeiChouShuiTiIndexBean;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.constant.Constant;
import com.zztrapp.zztrapp.databinding.ActivityHeiCouShuiTiBinding;
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.index.adapter.HeiCouShuiTiAdapter;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;

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

/**
 * Describe 黑臭水体
 * Created by Bby on 2022/6/26
 * Email bby0856@163.com
 */
public class HeiCouShuiTiActivity extends BaseActivity {

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

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

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

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

    @Override
    protected void init() {
        countyCode = getIntent().getStringExtra("countyCode");
        binding.navView.navTitle.setText(R.string.black_water);
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.refresh.setRefreshHeader(new ClassicsHeader(HeiCouShuiTiActivity.this));
        binding.refresh.setRefreshFooter(new ClassicsFooter(HeiCouShuiTiActivity.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(HeiCouShuiTiActivity.this, LinearLayoutManager.VERTICAL, false));
        adapter = new HeiCouShuiTiAdapter(HeiCouShuiTiActivity.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() {
        adapter.setOnItemClickListener(new HeiCouShuiTiAdapter.OnItemClickListener() {
            @Override
            public void onEditClick(String id, int position) {
                Intent intent = new Intent(HeiCouShuiTiActivity.this, HeiChouShuiTiEditActivity.class);
                intent.putExtra("jumpType", 1);//1编辑 2查看详情
                intent.putExtra("id", id);
                intent.putExtra("detail", list.get(position));
                startActivity(intent);
            }

            @Override
            public void onLookDetailClick(String id, int position) {
                Intent intent = new Intent(HeiCouShuiTiActivity.this, HeiChouShuiTiEditActivity.class);
                intent.putExtra("jumpType", 2);//1编辑 2查看详情
                intent.putExtra("id", id);
                intent.putExtra("detail", list.get(position));
                startActivity(intent);
            }

            @Override
            public void onDailyTasksClick(String id) {
                Intent intent1 = new Intent(HeiCouShuiTiActivity.this, DailyTasksActivity.class);
                intent1.putExtra("pid", id);
                startActivity(intent1);
            }
        });
        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<HeiChouShuiTiIndexBean>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<HeiChouShuiTiIndexBean>>() {
            @Override
            public void onNext(HttpResult<HeiChouShuiTiIndexBean> httpResult) {
                if (httpResult.getCode() == 200) {
                    total = httpResult.getResult().getPages();
                    if (isRefresh) {
                        list.clear();
                    }
                    list.addAll(httpResult.getResult().getRecords());
                    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<HeiChouShuiTiIndexBean>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, HeiCouShuiTiActivity.this, isShowDialog);
        RxApiManager.get().add("hcstPagelist", httpResultProgressSubscriber);
        HttpMethods.getInstance(HeiCouShuiTiActivity.this).hcstPagelist(httpResultProgressSubscriber, pageNo, Constant.PAGE_SIZE, countyCode, searchName);
    }

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

}