package com.dskj.trustlend.fragment;

import android.text.TextUtils;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.azhon.basic.base.BaseLazyFragment;
import com.dskj.trustlend.R;
import com.dskj.trustlend.adapter.comm.CommonAdapter;
import com.dskj.trustlend.adapter.comm.ViewHolder;
import com.dskj.trustlend.api.Api;
import com.dskj.trustlend.api.BaseObserver;
import com.dskj.trustlend.api.Result;
import com.dskj.trustlend.bean.ListBean;
import com.dskj.trustlend.bean.LoanBean;
import com.dskj.trustlend.databinding.FragmentStatusBinding;
import com.dskj.trustlend.home.MainViewModel;
import com.dskj.trustlend.utils.RxBus;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import rx.Subscription;


/**
 * 主页模块
 * @author xuhuixiang
 */
public class StatusFragment extends BaseLazyFragment<MainViewModel, FragmentStatusBinding>  {
    public static final String TAG = "StatusFragment";
    private ArrayList<LoanBean> lists = new ArrayList<>();
    CommonAdapter commonAdapter;
    int pageSize = 1;
    boolean isNeeDate = true;
    private Subscription mEventSubscription;

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_status;
    }

    @Override
    protected void setUpView() {
        initList();
        initRefreshLayout();
        mEventSubscription = RxBus.getInstance().toObservable(Integer.class)
                .subscribeOn(rx.schedulers.Schedulers.io())
                .unsubscribeOn(rx.schedulers.Schedulers.io())
                .subscribe(changeImageViewBean -> {//回顶部
                    if (changeImageViewBean == 77777) {
                        pageSize = 1;
                        dataBinding.refreshLayout.setEnableLoadMore(true);
                        getList();
                    }
                });
    }

    private void initRefreshLayout() {
        dataBinding.refreshLayout.setOnRefreshListener(refreshlayout -> {
            pageSize = 1;
            dataBinding.refreshLayout.setEnableLoadMore(true);
            getList();

        });
        dataBinding.refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            pageSize += 1;
            getList();
        });
    }
    private void getList() {
        Api.getInstance().getList(pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Result<ListBean>>() {

                    @Override
                    public void onSuccess(Result<ListBean> feedbackResp) {
                        dataBinding.refreshLayout.finishRefresh();
                        dataBinding.refreshLayout.finishLoadMore();
                        if (pageSize == 1) {
                            lists = (ArrayList<LoanBean>) feedbackResp.data.getData();
                            commonAdapter.setDates(lists);
                        } else {
                            lists.addAll(feedbackResp.data.getData());
                            commonAdapter.setDates(lists);
                        }
                        if (lists.size() == 0) {
                            isNeeDate = false;
                        } else {
                            isNeeDate = true;
                        }
//                        changeDate(false);
                        if (lists.size() == feedbackResp.data.getTotal()) {
                            dataBinding.refreshLayout.finishLoadMoreWithNoMoreData();
                        }
                    }

                    @Override
                    public void onError(int code, String msg) {
                        dataBinding.refreshLayout.finishRefresh();
                        dataBinding.refreshLayout.finishLoadMore();
                        if (lists.size() == 0) {
                            isNeeDate = false;
                        } else {
                            isNeeDate = true;
                        }
//                        changeDate(false);
                    }
                });
    }


    private void initList() {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        dataBinding.recyclerview.setLayoutManager(linearLayoutManager );
        commonAdapter = new CommonAdapter<LoanBean>(getContext(), R.layout.attack_child_item, lists) {
            @Override
            public void convert(ViewHolder holder, LoanBean s, int index) {
                holder.setText(R.id.amout_tv,"HKD "+s.getAmount());
//                holder.setText(R.id.id_tv,"Order ID: "+s.getId());
                holder.setText(R.id.ctime_tv,s.getCreate_time());

                TextView statusTv = holder.getView(R.id.statusTv);
                statusTv.setBackgroundResource(R.drawable.button_jindu_true);
                if(s.getStatus() == 0){
                    holder.setText(R.id.statusTv,"Processing");
                    statusTv.setTextColor(getResources().getColor(R.color.color_1177C9));
                    statusTv.setBackgroundResource(R.drawable.button_jindu_true2);
                    holder.setText(R.id.salary_tv,"");
                }else if(s.getStatus() == 1){
                    holder.setText(R.id.statusTv,"Approve");
                    statusTv.setTextColor(getResources().getColor(R.color.color_178038));
                    holder.setText(R.id.salary_tv,"HKD "+s.getSalary());

                }else if(s.getStatus() == 2){
                    holder.setText(R.id.statusTv,"Fail");
                    statusTv.setTextColor(getResources().getColor(R.color.color_search_2));
                    statusTv.setBackgroundResource(R.drawable.button_jindu_true1);
                    holder.setText(R.id.salary_tv,"");

                }
                if(TextUtils.isEmpty(s.getUpdate_time())){
                    holder.setText(R.id.etime_tv,"");

                }else{
                    holder.setText(R.id.etime_tv,s.getUpdate_time());
                }

                switch (s.getType()){
                    case "1":
                        holder.setText(R.id.type_tv,"Daily");
                        holder.setText(R.id.shichang_tv,s.getPeriods()+" Days");
                        break;
                    case "2":
                        holder.setText(R.id.type_tv,"Weekly");
                        holder.setText(R.id.shichang_tv,s.getPeriods()+" Weeks");
                        break;
                    default:
                        holder.setText(R.id.type_tv,"Monthly");
                        holder.setText(R.id.shichang_tv,s.getPeriods()+" Months");
                        break;
                }
            }
        };
        dataBinding.recyclerview.setAdapter(commonAdapter);
    }
    @Override
    public void onResume() {
        super.onResume();

    }


    @Override
    protected void setUpData() {
        dataBinding.setModel(viewModel);
        dataBinding.setLifecycleOwner(this);
    }


    @Override
    protected MainViewModel initViewModel() {
        return ViewModelProviders.of(getActivity()).get(MainViewModel.class);
    }

    @Override
    protected void showError(Object obj) {

    }

    @Override
    protected void lazyLoad() {
        dataBinding.refreshLayout.autoRefresh();

    }


    @Override
    public void onDestroy() {
        mEventSubscription.unsubscribe();
        super.onDestroy();

    }

}
