package com.tofly.ghrq_route.common.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.C;
import com.tofly.ghrq_route.common.ui.adapter.BuildHandleAdapter;
import com.tofly.ghrq_route.module.buildManage.controller.BuildManageCallback;
import com.tofly.ghrq_route.module.buildManage.controller.BuildManageController;
import com.tofly.ghrq_route.module.buildManage.domain.BuildHandleBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.ui.activity.UiBuildHandleDetail;
import com.tofly.ghrq_route.common.util.EventBusUtil;

import org.greenrobot.eventbus.Subscribe;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Amu on 2017/5/24.
 */

/*监护申请，未使用*/
public class BuildHandleFragment extends Fragment {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.empty_view)
    RelativeLayout layoutEmptyView;
    Unbinder unbinder;

    private static final String TAG = BuildHandleFragment.class.getName();

    private Context mContext;

    private View mRootView;

    private UserBean mUser;

    private BuildManageController buildManageController;

    private List<BuildHandleBean> mBuildHandleList;

    private BuildHandleAdapter buildHandleAdapter;

    private BuildManageBean mBuildManage;

    private boolean mbIsRefreshing = false;

    private int pageindex = 1;

    private final int pagesize = C.DEFAULT_MAX_LOAD_PAGESIZE;

    public BuildHandleFragment(BuildManageBean buildManage) {
        mBuildManage = buildManage;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mUser = BaseApp.getInstance().getLoginUser();

        EventBusUtil.register(this);
    }

    @Nullable @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mContext = getActivity();
            mRootView = inflater.inflate(R.layout.fragment_build_report, container, false);
        } else {
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (parent != null) {
                parent.removeView(mRootView);
            }
        }
        unbinder = ButterKnife.bind(this, mRootView);
        return mRootView;
    }

    @Subscribe
    public void onMessageEvent(BuildHandleEvent event){
        if (event.message.equals(BUILD_HANDLE_REMOVED) || event.message.equals(BUILD_HANDLE_UPLOADED)){
            swipeRefreshLayout.setRefreshing(true);
            loadBuildHandleList();
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initData();
        initListener();
    }

    private void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        layoutEmptyView.setVisibility(View.VISIBLE);
    }

    private void initData(){
        mBuildHandleList = new ArrayList<>();
        buildHandleAdapter = new BuildHandleAdapter(mContext, mBuildHandleList);
        recyclerView.setAdapter(buildHandleAdapter);
        swipeRefreshLayout.setRefreshing(true);
        loadBuildHandleList();
    }

    private void initListener(){
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (mbIsRefreshing){
                }else {
                    loadBuildHandleList();
                }
            }
        });

        if (buildHandleAdapter != null){
            buildHandleAdapter.setOnItemClickListener(new ARecyclerBaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    BuildHandleBean handleBean = buildHandleAdapter.getItem(position);
                    if (handleBean != null){
                        Intent intent = new Intent();
                        intent.setClass(mContext, UiBuildHandleDetail.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("build_manage", mBuildManage);
                        bundle.putSerializable("build_handle", handleBean);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }
            });
        }
    }

    private void loadBuildHandleList(){
        if (mBuildManage != null){
            if (buildManageController == null)
                buildManageController = new BuildManageController(mContext);
            buildManageController.getBuildHandleList(mUser.getToken(), mBuildManage.getId(), pageindex, pagesize, new BuildManageCallback(){
                @Override
                public void onGetBuildHandleList(boolean isSuccess, int errorCode, String errMsg, List<BuildHandleBean> handleList, int total) {
                    super.onGetBuildHandleList(isSuccess, errorCode, errMsg, handleList, total);
                    if (swipeRefreshLayout != null){
                        swipeRefreshLayout.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                swipeRefreshLayout.setRefreshing(false);
                                mbIsRefreshing = false;
                            }
                        }, 500);
                    }
                    recyclerView.setVisibility(View.GONE);
                    layoutEmptyView.setVisibility(View.VISIBLE);
                    if (isSuccess && handleList != null){
                        mBuildHandleList = handleList;
                        buildHandleAdapter.setData(handleList);
                        if (handleList.size() > 0){
                            recyclerView.setVisibility(View.VISIBLE);
                            layoutEmptyView.setVisibility(View.GONE);
                        }
                    }else {
                        ToastUtil.showToast(mContext, errMsg);
                    }
                }
            });
        }
    }

    public final static String BUILD_HANDLE_REMOVED = "buildHandleRemoved";
    public final static String BUILD_HANDLE_UPLOADED= "buildHandleUploaded";
    public static class BuildHandleEvent {
        public String message;
        public BuildHandleEvent(String message) {
            this.message = message;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        EventBusUtil.unregister(this);
        super.onDestroy();
    }
}
