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

import android.app.Activity;
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.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.common.ui.activity.UiBuildManageDetail;
import com.tofly.ghrq_route.common.ui.activity.UiBuildReportDetail;
import com.tofly.ghrq_route.common.ui.adapter.BuildReportAdapter;
import com.tofly.ghrq_route.module.buildManage.BuildTool;
import com.tofly.ghrq_route.module.buildManage.controller.BuildManageCallback;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildReportBean;
import com.tofly.ghrq_route.module.buildManage.domain.BuildReportEvent;
import com.tofly.ghrq_route.common.util.EventBusUtil;

import org.greenrobot.eventbus.EventBus;
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 BuildReportFragment 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 Activity mContext;

    private View mRootView;

    private BuildTool buildTool;

    private List<BuildReportBean> mBuildReportList;

    private BuildReportAdapter buildReportAdapter;

    private BuildManageBean mBuildManage;

    private boolean mbIsRefreshing = false;

    public BuildReportFragment() {
    }

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

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

        EventBusUtil.register(this);
    }

    @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(BuildReportEvent event){
        if (event.message.equals(BuildReportEvent.BUILD_REPORT_REMOVED)){
            swipeRefreshLayout.setRefreshing(true);
            loadBuildReportList();
        }else if (event.message.equals(BuildReportEvent.BUILD_REPORT_UPLOADED) ||
                event.message.equals(BuildReportEvent.BUILD_REPORT_SAVED)){
            int buildState = (int)event.arg;
            if (buildState == 1){
                EventBus.getDefault().post(new UiBuildManageDetail.BuildManageDetailEvent(UiBuildManageDetail.BUILD_MANAGE_NEED_UPDATE));
                mContext.finish();
            }else {
                swipeRefreshLayout.setRefreshing(true);
                loadBuildReportList();
            }
        }
    }

    @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(){
        buildTool = BuildTool.getInstance();
        mBuildReportList = new ArrayList<>();
        buildReportAdapter = new BuildReportAdapter(mContext, mBuildReportList, mBuildManage);
        recyclerView.setAdapter(buildReportAdapter);
        swipeRefreshLayout.setRefreshing(true);
        loadBuildReportList();
    }

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

        if (buildReportAdapter != null){
            buildReportAdapter.setOnItemClickListener(new ARecyclerBaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    BuildReportBean reportBean = buildReportAdapter.getItem(position);
                    if (reportBean != null){
                        Intent intent = new Intent();
                        intent.setClass(mContext, UiBuildReportDetail.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("selected_position", position);
                        bundle.putSerializable("build_manage", mBuildManage);
                        bundle.putSerializable("build_report", reportBean);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                }
            });
        }
    }

    private void loadBuildReportList(){
        if (mBuildManage != null){
            buildTool.getBuildReportList(mContext, mBuildManage, new BuildManageCallback(){
                @Override
                public void onGetBuildReportList(boolean isSuccess, int errorCode, String errMsg, List<BuildReportBean> buildReportList, int total) {
                    super.onGetBuildReportList(isSuccess, errorCode, errMsg, buildReportList, total);
                    stopRefreshing();
                    recyclerView.setVisibility(View.GONE);
                    layoutEmptyView.setVisibility(View.VISIBLE);
                    if (isSuccess && buildReportList != null){
                        mBuildReportList = buildReportList;
                        buildReportAdapter.setData(buildReportList);
                        if (buildReportList.size() > 0){
                            recyclerView.setVisibility(View.VISIBLE);
                            layoutEmptyView.setVisibility(View.GONE);
                        }
                    }
                }
            });
        }
    }

    private void stopRefreshing() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (swipeRefreshLayout != null)
                        swipeRefreshLayout.setRefreshing(false);
                    mbIsRefreshing = false;
                }
            }, 500);
        }
    }

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

    @Override
    public void onDestroy() {
        EventBusUtil.unregister(this);
        BaseApp.cancelAllRequest(mContext.getClass().getName());
        super.onDestroy();
    }
}
