package com.android.chmo.ui.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.android.chmo.R;
import com.android.chmo.base.BaseFragment;
import com.android.chmo.ui.activity.model.SearchModelActivity;
import com.android.chmo.ui.fragment.home.ActivitiesFragment;
import com.android.chmo.ui.fragment.home.ActorFragment;
import com.android.chmo.ui.fragment.home.FollowFragment;
import com.android.chmo.ui.fragment.home.RecommendFragment;
import com.android.chmo.utils.CommonUtils;
import com.android.chmo.utils.PixelUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.android.chmo.R.id.pager;

/**
 * Created by Administrator on 2018/7/31.
 */

public class HomeFragment extends BaseFragment {
    private final int PAGE_FOLLOW = 0;
    private final int PAGE_RECOMMEND = 1;
    private final int PAGE_ACTOR = 2;
    private final int PAGE_ACTIVITY = 3;

    @BindView(R.id.top)
    View topBar;
    @BindView(R.id.tab_follow)
    View tabFollow;
    @BindView(R.id.tab_recommend)
    View tabRecommend;
    @BindView(R.id.tab_actor)
    View tabActor;
    @BindView(R.id.tab_activity)
    View tabActivity;


    @BindView(pager)
    ViewPager viewPager;

    private HomePagerAdapter pagerAdapter;

    private View curTab;

    private int curPage = 1;


    @Override
    public int getLayoutRes() {
        return R.layout.page_home;
    }

    @Override
    public void initView() {
        initTopBar();

        pagerAdapter = new HomePagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(4);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                curPage = position;
                changeTab();
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        changeTab();
        viewPager.setCurrentItem(curPage, false);
    }

    void initTopBar() {
        int statusBarHeight = CommonUtils.getStatusBarHeight(getBaseActivity());
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) topBar.getLayoutParams();
        lp.height = PixelUtils.dp2px(55, getContext()) + statusBarHeight;
        topBar.setPadding(0, statusBarHeight, 0, 0);
        topBar.setLayoutParams(lp);
    }

    @OnClick({R.id.tab_follow, R.id.tab_actor, R.id.tab_activity, R.id.tab_recommend})
    public void onTabClick(View v) {
        if (null != curTab) {
            curTab.setSelected(false);
        }
        curTab = v;
        curTab.setSelected(true);
        switch (v.getId()) {
            case R.id.tab_follow:
                curPage = PAGE_FOLLOW;
                break;
            case R.id.tab_recommend:
                curPage = PAGE_RECOMMEND;
                break;
            case R.id.tab_actor:
                curPage = PAGE_ACTOR;
                break;
            case R.id.tab_activity:
                curPage = PAGE_ACTIVITY;
                break;

            default:
                break;
        }
        viewPager.setCurrentItem(curPage, true);
    }

    private void changeTab() {
        if(curTab != null) {
            curTab.setSelected(false);
        }
        switch (curPage) {
            case PAGE_FOLLOW:
                curTab = tabFollow;
                break;
            case PAGE_RECOMMEND:
                curTab = tabRecommend;
                break;
            case PAGE_ACTOR:
                curTab = tabActor;
                break;
            case PAGE_ACTIVITY:
                curTab = tabActivity;
                break;
        }
        curTab.setSelected(true);
    }

    @OnClick(R.id.search)
    public void search() {
        openPage(SearchModelActivity.class);
    }


    private class HomePagerAdapter extends FragmentPagerAdapter {

        public HomePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case PAGE_FOLLOW:
                    return Fragment.instantiate(getActivity(),
                            FollowFragment.class.getName());
                case PAGE_RECOMMEND:
                    return Fragment.instantiate(getActivity(),
                            RecommendFragment.class.getName());
                case PAGE_ACTOR:
                    return Fragment.instantiate(getActivity(),
                            ActorFragment.class.getName());
                case PAGE_ACTIVITY:
                    return Fragment.instantiate(getActivity(),
                            ActivitiesFragment.class.getName());

            }

            throw new RuntimeException("undefine position:" + position);
        }

        @Override
        public int getCount() {
            return 4;
        }
    }
}
