package kr.co.hanwha.hifive.screen.main.home;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.MainHomeAssignedListAdapter;
import kr.co.hanwha.hifive.adapter.MainHomeRequestListAdapter;
import kr.co.hanwha.hifive.base.BaseFragment;
import kr.co.hanwha.hifive.data.model.TaskData;
import kr.co.hanwha.hifive.data.model.TaskListData;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.view.HFMainTabView;
import kr.co.hanwha.hifive.view.refresh.CustomRefreshLayout;
import kr.co.hanwha.hifive.view.refresh.OnRefreshListener;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;

/**
 * 메인 - HOME View
 *
 * @see MainHomePresenter
 * Created by sanghoonkwon on 2018. 1. 24..
 */

public class MainHomeFragment extends BaseFragment implements MainHomeContract.View {
    private ViewPager mViewPager;
    private View mViewAssigned;
    private View mViewRequest;
    private CustomRefreshLayout mRefreshLayoutAssigned;
    private CustomRefreshLayout mRefreshLayoutRequest;
    private RecyclerView mRecyclerViewAssigned;
    private RecyclerView mRecyclerViewRequest;
    private MainHomeAssignedListAdapter mAdapterAssigned;
    private MainHomeRequestListAdapter mAdapterRequest;
    private HFMainTabView mMainTabView;

    private MainHomePresenter mPresenter;

    private final int INDEX_MINE = 0;
    private final int INDEX_REQUEST = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View parent = inflater.inflate(R.layout.fragment_main_home, container, false);

        mMainTabView = (HFMainTabView) parent.findViewById(R.id.mainTabView);
        mMainTabView.setOnMainTabListener(onMainTabListener);

        LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
        mViewAssigned = layoutInflater.inflate(R.layout.temp_fragment_main_home_sub, container, false);
        mViewRequest = layoutInflater.inflate(R.layout.temp_fragment_main_home_sub, container, false);

        mAdapterAssigned = new MainHomeAssignedListAdapter(getActivity(), onMainHomeListListener);

        mRefreshLayoutAssigned = (CustomRefreshLayout) mViewAssigned.findViewById(R.id.customRefreshLayout);
        mRefreshLayoutAssigned.setLoadEnable(false);
        mRefreshLayoutAssigned.setOnRefreshListener(onMineRefreshListener);

        mRecyclerViewAssigned = (RecyclerView) mViewAssigned.findViewById(R.id.recyclerView);
        mRecyclerViewAssigned.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerViewAssigned.setAdapter(mAdapterAssigned);

        mAdapterRequest = new MainHomeRequestListAdapter(getActivity(), onMainHomeListListener);

        mRefreshLayoutRequest = (CustomRefreshLayout) mViewRequest.findViewById(R.id.customRefreshLayout);
        mRefreshLayoutRequest.setLoadEnable(false);
        mRefreshLayoutRequest.setOnRefreshListener(onRequestRefreshListener);

        mRecyclerViewRequest = (RecyclerView) mViewRequest.findViewById(R.id.recyclerView);
        mRecyclerViewRequest.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerViewRequest.setAdapter(mAdapterRequest);

        mViewPager = (ViewPager) parent.findViewById(R.id.viewPager);
        MainPagerAdapter pagerAdapter = new MainPagerAdapter();
        mViewPager.setAdapter(pagerAdapter);
        mViewPager.addOnPageChangeListener(onPageChangeListener);
        mViewPager.setOffscreenPageLimit(2);

        parent.findViewById(R.id.alarm_imageButton).setOnClickListener(onClickListener);
        parent.findViewById(R.id.search_imageButton).setOnClickListener(onClickListener);

        mPresenter = new MainHomePresenter(getActivity(), this,
                mAdapterAssigned, mAdapterAssigned,
                mAdapterRequest, mAdapterRequest);
        mPresenter.initData();

        return parent;
    }

    @Override
    public void refreshData() {
        mPresenter.refreshData();
    }

    @Override
    public void stopRefreshAssigned() {
        mRefreshLayoutAssigned.stopRefresh(true);
    }

    @Override
    public void stopRefreshRequest() {
        mRefreshLayoutRequest.stopRefresh(true);
    }

    /**************************************************
     * View.OnClickListener
     **************************************************/
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.alarm_imageButton:
                    HFActivityUtil.startAlarmActivity(getActivity());
                    break;
                case R.id.search_imageButton:
                    HFActivityUtil.startSearchActivity(getActivity());
                    break;
            }

        }
    };

    /**************************************************
     * OnRefreshListener
     **************************************************/
    OnRefreshListener onMineRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPresenter.initDataAssigned(false);
        }

        @Override
        public void onLoadMore() {

        }
    };

    /**************************************************
     * OnRefreshListener
     **************************************************/
    OnRefreshListener onRequestRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPresenter.initDataRequested(false);
        }

        @Override
        public void onLoadMore() {

        }
    };

    /**************************************************
     * HFMainTabView.OnMainTabListener
     **************************************************/
    HFMainTabView.OnMainTabListener onMainTabListener = new HFMainTabView.OnMainTabListener() {
        @Override
        public void onTabSelected(int position) {
            mViewPager.setCurrentItem(position);
        }
    };

    /**************************************************
     * MainHomeContract.OnMainHomeListListener
     **************************************************/
    MainHomeContract.OnMainHomeListListener onMainHomeListListener = new MainHomeContract.OnMainHomeListListener() {
        @Override
        public void onItemClicked(@NonNull TaskData data) {
            HFActivityUtil.startTaskDetailActivity(getActivity(), data);
        }

        @Override
        public void updateTodoTaskToTodayTask(@NonNull TaskData data, boolean isChecked) {
            mPresenter.updateTodoTaskToTodayTask(data, isChecked);
        }

        @Override
        public void onTaskAcceptClicked(@NonNull TaskData data) {
            mPresenter.doTaskAccept(data);
        }

        @Override
        public void onTaskRejectClicked(@NonNull TaskData data) {
            mPresenter.doTaskReject(data);
        }
    };

    /**************************************************
     * ViewPager.OnPageChangeListener
     **************************************************/
    ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            mMainTabView.setCurrentItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    /**************************************************
     * PagerAdapter
     **************************************************/
    class MainPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (position == INDEX_MINE) {
                container.addView(mViewAssigned);
                return mViewAssigned;
            } else {
                container.addView(mViewRequest);
                return mViewRequest;
            }
        }
    }
}