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

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.lcp.arecyclerview.widget.ARecycleView;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.common.ui.adapter.JobListPageAdapter;
import com.tofly.ghrq_route.common.ui.adapter.SketchListAdapter;
import com.tofly.ghrq_route.module.sketch.SketchTool;
import com.tofly.ghrq_route.module.sketch.controller.SketchCallback;
import com.tofly.ghrq_route.module.sketch.domain.MySketchEvent;
import com.tofly.ghrq_route.module.sketch.domain.SketchBean;
import com.tofly.ghrq_route.common.util.EventBusUtil;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

public class UiSketchList extends BaseUi {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private int mSelectState = 0;

    private ARecycleView recyclerView;

    private JobListPageAdapter pageAdapter;

    private SketchListAdapter sketchListAdapter;

    private SketchTool sketchTool;

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

        setContentView(R.layout.ui_job_list);
        ButterKnife.bind(this);

        initView();
        initData();
        initListener();

        EventBusUtil.register(this);
    }

    private void initView() {
        initTabLayout();
    }

    private void initTabLayout() {
        String[] tabTitle = getResources().getStringArray(R.array.sketch_select_state);
        List<String> labelList = Arrays.asList(tabTitle);
        pageAdapter = new JobListPageAdapter(this, labelList);
        viewPager.setAdapter(pageAdapter);
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < pageAdapter.getCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null)
                tab.setText(tabTitle[i]);
        }
    }

    private void initData() {
        sketchTool = SketchTool.getInstance();

        ArrayList<SketchBean> list = new ArrayList<>();
        sketchListAdapter = new SketchListAdapter(this, list);

        recyclerView = pageAdapter.getRecycleView(mSelectState);
        if (recyclerView != null) {
            recyclerView.setAdapter(sketchListAdapter);
            recyclerView.setOnRefreshListener(this::getOnlineSketchList);
            getOnlineSketchList();
        }
    }

    private void initListener() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                recyclerView = pageAdapter.getRecycleView(position);
                mSelectState = position;
                if (recyclerView != null) {
                    sketchListAdapter.clear();
                    recyclerView.setAdapter(sketchListAdapter);
                    recyclerView.setOnRefreshListener(UiSketchList.this::getOnlineSketchList);
                    Handler handler = new Handler();
                    handler.postDelayed(() -> getOnlineSketchList(), 100);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

/*        recyclerView.setOnLoadMoreListener(new ARecycleView.OnLoadMoreListener() {
            @Override
            public void loadMore() {
                pageindex++;
                getLocalPlanList();
            }
        });*/

        if (sketchListAdapter != null) {
            sketchListAdapter.setOnItemClickListener((view, position) -> {
                SketchBean sketchBean = sketchListAdapter.getItem(position);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable(SketchBean.SKETCH_BEAN_KEY, sketchBean);
                intent.setClass(UiSketchList.this, UiSketchUpload.class);
                intent.putExtras(bundle);
                startActivity(intent);
            });
        }
    }

    @Subscribe
    public void onMessageEvent(MySketchEvent event){
        if (MySketchEvent.MY_SKETCH_LIST_NEED_REFRESH.equals(event.message)) {
            getOnlineSketchList();
        }
    }

    private void startRefreshing(){
        recyclerView.setRefreshing(true);
    }

    private void stopRefreshing(){
        Handler handler = new Handler();
        handler.postDelayed(() -> {
            recyclerView.setRefreshing(false);
            recyclerView.hideMoreProgress();
        }, 500);
    }

    private void getOnlineSketchList(){
        startRefreshing();
        sketchTool.getSketchListOnline(this, new SketchCallback(){
            @Override
            public void onGetSketchList(boolean isSuccess, int errorCode, String errMsg, List<SketchBean> sketchList, int total) {
                super.onGetSketchList(isSuccess, errorCode, errMsg, sketchList, total);
                stopRefreshing();
                /*加载本地保存的计划*/
                getLocalSketchList();
            }
        });
    }

    private void getLocalSketchList() {
        int job_state = mSelectState;
        sketchTool.getSketchListOffline(this, job_state, new SketchCallback(){
            @Override
            public void onGetSketchList(boolean isSuccess, int errorCode, String errMsg, List<SketchBean> sketchList, int total) {
                super.onGetSketchList(isSuccess, errorCode, errMsg, sketchList, total);
                if (isSuccess){
                    recyclerView.setTotal(total);
                    sketchListAdapter.clear();
                    if (sketchList != null) {
                        sketchListAdapter.setData(sketchList, mSelectState);
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBusUtil.unregister(this);
    }
}
