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

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.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.MainDashboardListAdapter;
import kr.co.hanwha.hifive.adapter.contract.MainDashboardListContract;
import kr.co.hanwha.hifive.base.BaseFragment;
import kr.co.hanwha.hifive.data.model.DabProjectData;
import kr.co.hanwha.hifive.screen.main.dashboard.contract.MainDashboardContract;
import kr.co.hanwha.hifive.util.HFActivityUtil;
import kr.co.hanwha.hifive.util.HFInfoUtil;
import kr.co.hanwha.hifive.view.HFDashboardTabView;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;

/**
 * Created by sanghoonkwon on 2018. 3. 15..
 */

public class MainDashboardFragment extends BaseFragment implements MainDashboardContract.View {
    private ViewPager mViewPager;
    private View mViewGreen;
    private View mViewOrange;
    private SwipeRefreshLayout mRefreshLayoutGreen;
    private SwipeRefreshLayout mRefreshLayoutOrange;
    private RecyclerView mRecyclerViewGreen;
    private RecyclerView mRecyclerViewOrange;
    private MainDashboardListAdapter mAdapterGreen;
    private MainDashboardListAdapter mAdapterOrange;
    private GridLayoutManager mLayoutManagerGreen;
    private GridLayoutManager mLayoutManagerOrange;
    private TextView mTextViewEmptyGreen;
    private TextView mTextViewEmptyOrange;

    private HFDashboardTabView mDashboardTabView;

    private MainDashboardPresenter mPresenter;

    private final int INDEX_ORANGE = 0;
    private final int INDEX_GREEN = 1;

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

        mDashboardTabView = (HFDashboardTabView) parent.findViewById(R.id.dashboardTabView);
        mDashboardTabView.setOnMainTabListener(onMainTabListener);

        LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
        mViewGreen = layoutInflater.inflate(R.layout.fragment_main_dashboard_sub, container, false);
        mViewOrange = layoutInflater.inflate(R.layout.fragment_main_dashboard_sub, container, false);

        //Green
        mAdapterGreen = new MainDashboardListAdapter(getActivity(), onMainDashboardListListener);
        mRefreshLayoutGreen = (SwipeRefreshLayout) mViewGreen.findViewById(R.id.swipeRefreshLayout);
        mRefreshLayoutGreen.setOnRefreshListener(onRefreshListener);

        mLayoutManagerGreen = new GridLayoutManager(getActivity(), 2);
        mRecyclerViewGreen = (RecyclerView) mViewGreen.findViewById(R.id.recyclerView);
        mRecyclerViewGreen.setLayoutManager(mLayoutManagerGreen);
        mRecyclerViewGreen.setAdapter(mAdapterGreen);

        mTextViewEmptyGreen = (TextView) mViewGreen.findViewById(R.id.empty_textView);

        // Orange
        mAdapterOrange = new MainDashboardListAdapter(getActivity(), onMainDashboardListListener);
        mRefreshLayoutOrange = (SwipeRefreshLayout) mViewOrange.findViewById(R.id.swipeRefreshLayout);
        mRefreshLayoutOrange.setOnRefreshListener(onRefreshListener);

        mLayoutManagerOrange = new GridLayoutManager(getActivity(), 2);
        mRecyclerViewOrange = (RecyclerView) mViewOrange.findViewById(R.id.recyclerView);
        mRecyclerViewOrange.setLayoutManager(mLayoutManagerOrange);
        mRecyclerViewOrange.setAdapter(mAdapterOrange);

        mTextViewEmptyOrange = (TextView) mViewOrange.findViewById(R.id.empty_textView);

        mLayoutManagerOrange.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return (mAdapterOrange.getFullSpan(position) ? 2 : 1);
            }
        });
        mLayoutManagerGreen.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return (mAdapterGreen.getFullSpan(position) ? 2 : 1);
            }
        });

        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 MainDashboardPresenter(getActivity(), this,
                mAdapterGreen, mAdapterGreen,
                mAdapterOrange, mAdapterOrange);
        mPresenter.initData(true);

        return parent;
    }

    @Override
    public void onResume() {
        super.onResume();

        if (HFInfoUtil.getNeedRefreshMainDashboardList(getActivity())) {
            mPresenter.initData(true);
        }
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        mRefreshLayoutGreen.setRefreshing(refreshing);
        mRefreshLayoutOrange.setRefreshing(refreshing);
    }

    @Override
    public void setEmptyViewOrange(boolean empty) {
        if (empty) {
            mTextViewEmptyOrange.setVisibility(View.VISIBLE);
            mRefreshLayoutOrange.setVisibility(View.GONE);
        } else {
            mTextViewEmptyOrange.setVisibility(View.GONE);
            mRefreshLayoutOrange.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void setEmptyViewGreen(boolean empty) {
        if (empty) {
            mTextViewEmptyGreen.setVisibility(View.VISIBLE);
            mRefreshLayoutGreen.setVisibility(View.GONE);
        } else {
            mTextViewEmptyGreen.setVisibility(View.GONE);
            mRefreshLayoutGreen.setVisibility(View.VISIBLE);
        }
    }

    /**************************************************
     * 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
     **************************************************/
    SwipeRefreshLayout.OnRefreshListener onRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPresenter.initData(false);
        }
    };

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

    /**************************************************
     * MainDashboardListContract.OnMainDashboardListListener
     **************************************************/
    MainDashboardListContract.OnMainDashboardListListener onMainDashboardListListener = new MainDashboardListContract.OnMainDashboardListListener() {
        @Override
        public void onItemClicked(@NonNull DabProjectData data) {
            if (data == null) return;
            HFActivityUtil.startDashboardProjectActivity(getActivity(), data.getProject_id());
        }
    };

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

        @Override
        public void onPageSelected(int position) {
            mDashboardTabView.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_GREEN) {
                container.addView(mViewGreen);
                return mViewGreen;
            } else {
                container.addView(mViewOrange);
                return mViewOrange;
            }
        }
    }
}