package com.itant.joycity.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.itant.joycity.R;
import com.itant.joycity.adapter.FragmentAdapter;
import com.itant.joycity.fragment.first.NewestFragment;
import com.itant.joycity.fragment.first.OfficialFragment;
import com.itant.joycity.fragment.first.VideoFragment;
import com.itant.joycity.tool.UITool;
import com.shizhefei.view.indicator.Indicator;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.slidebar.LayoutBar;
import com.shizhefei.view.indicator.slidebar.ScrollBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jason on 2016/10/6.
 */
public class FirstFragment extends BaseFragment implements View.OnClickListener {
    private ViewPager nsvp_news;
    private FragmentAdapter mAdapter;
    private List<Fragment> mFragments;

    private LayoutInflater inflate;
    private IndicatorViewPager indicatorViewPager;

    private TextView tv_newest;
    private TextView tv_official;
    private TextView tv_video;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        inflate = LayoutInflater.from(getActivity());

        View view = inflater.inflate(R.layout.fragment_first, null);
        nsvp_news = (ViewPager) view.findViewById(R.id.nsvp_news);
        mFragments = new ArrayList<>();
        mFragments.add(new NewestFragment());
        mFragments.add(new OfficialFragment());
        mFragments.add(new VideoFragment());
        mAdapter = new FragmentAdapter(getChildFragmentManager(), mFragments);
        nsvp_news.setAdapter(mAdapter);
        nsvp_news.setOffscreenPageLimit(3);


        Indicator fiv_first_fragment = (Indicator) view.findViewById(R.id.fiv_first_fragment);
        fiv_first_fragment.setScrollBar(new LayoutBar(getActivity(), R.layout.layout_slide_bar, ScrollBar.Gravity.BOTTOM_FLOAT));

        float unSelectSize = UITool.dpToPx(getActivity(), 16);
        float selectSize = unSelectSize * 1.05f;

        int selectColor = getActivity().getResources().getColor(R.color.bg_btn);
        int unSelectColor = getActivity().getResources().getColor(R.color.white);
        fiv_first_fragment.setOnTransitionListener(new OnTransitionTextListener().setColor(selectColor, unSelectColor).setSize(selectSize, unSelectSize));
        indicatorViewPager = new IndicatorViewPager(fiv_first_fragment, nsvp_news);
        indicatorViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
        indicatorViewPager.setOnIndicatorPageChangeListener(new IndicatorViewPager.OnIndicatorPageChangeListener() {
            @Override
            public void onIndicatorPageChange(int preItem, int currentItem) {
                if (preItem != currentItem) {
                    switch (currentItem) {
                        case 0:
                            tv_newest.setTextColor(getResources().getColor(R.color.bg_btn));
                            tv_official.setTextColor(getResources().getColor(R.color.white));
                            tv_video.setTextColor(getResources().getColor(R.color.white));
                            break;
                        case 1:
                            tv_newest.setTextColor(getResources().getColor(R.color.white));
                            tv_official.setTextColor(getResources().getColor(R.color.bg_btn));
                            tv_video.setTextColor(getResources().getColor(R.color.white));
                            break;
                        case 2:
                            tv_newest.setTextColor(getResources().getColor(R.color.white));
                            tv_official.setTextColor(getResources().getColor(R.color.white));
                            tv_video.setTextColor(getResources().getColor(R.color.bg_btn));
                            break;
                        default:
                            break;
                    }
                }
            }
        });

        tv_newest = (TextView) view.findViewById(R.id.tv_newest);
        tv_newest.setTextColor(getResources().getColor(R.color.bg_btn));
        tv_newest.setOnClickListener(this);
        tv_official = (TextView) view.findViewById(R.id.tv_official);
        tv_official.setOnClickListener(this);
        tv_video = (TextView) view.findViewById(R.id.tv_video);
        tv_video.setOnClickListener(this);
        return view;
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_newest:
                indicatorViewPager.setCurrentItem(0, true);
                break;
            case R.id.tv_official:

                indicatorViewPager.setCurrentItem(1, true);
                break;
            case R.id.tv_video:

                indicatorViewPager.setCurrentItem(2, true);
                break;
            default:
                break;
        }
    }

    String[] topTabTitles = {"", "", ""};
    /**
     * 没有标题，只有箭头的viewpagerindicator
     */
    private class MyAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {

        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return topTabTitles.length;
        }

        @Override
        public View getViewForTab(int position, View convertView, ViewGroup container) {
            if (convertView == null) {
                convertView = inflate.inflate(R.layout.tab_top, container, false);
            }
            TextView textView = (TextView) convertView;
            textView.setText(topTabTitles[position]);
            return convertView;
        }

        @Override
        public Fragment getFragmentForPage(int position) {
            return mFragments.get(position);
        }
    }
}
