package com.gh.gamecenter.news;

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.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gh.base.HomeFragment;
import com.gh.common.util.DisplayUtils;
import com.gh.gamecenter.R;
import com.gh.gamecenter.eventbus.EBUISwitch;

import de.greenrobot.event.EventBus;

/**
 * Created by khy on 2016/6/29.
 */
public class NewsFragment extends HomeFragment implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private TextView tv_zixun;
    private TextView tv_yuanchuang;
    private TextView tv_gonglve;
    private TextView tv_guanzhu;

    private int width;
    private int currentItem;

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("currentItem", currentItem);
    }

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

        initTextView();

        home_vp_content.setAdapter(new FragmentAdapter(getChildFragmentManager()));
        home_vp_content.addOnPageChangeListener(this);

        currentItem = 0;
        if (savedInstanceState != null) {
            currentItem = savedInstanceState.getInt("currentItem");
        }
        home_vp_content.setCurrentItem(currentItem);

        DisplayMetrics outMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay()
                .getMetrics(outMetrics);
        width = outMetrics.widthPixels / 4;
        lparams = new LinearLayout.LayoutParams(width / 2,
                DisplayUtils.dip2px(getActivity(), 2));
        lparams.leftMargin = width * currentItem + width / 4;
        home_slide_line.setLayoutParams(lparams);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                EventBus.getDefault().post(new EBUISwitch("NewsFragment", home_vp_content.getCurrentItem()));
            }
        }, 100);
    }

    private void initTextView() {
        tv_zixun = new TextView(getActivity());
        tv_zixun.setText("资讯");
        tv_zixun.setTextColor(getResources().getColor(R.color.theme));
        tv_zixun.setGravity(Gravity.CENTER);
        tv_zixun.setOnClickListener(this);
        LinearLayout.LayoutParams lparams1 = new LinearLayout.LayoutParams(
                0, DisplayUtils.dip2px(getActivity(), 35));
        lparams1.weight = 1;
        home_ll_top.addView(tv_zixun, lparams1);

        tv_yuanchuang = new TextView(getActivity());
        tv_yuanchuang.setText("原创");
        tv_yuanchuang.setTextColor(getResources().getColor(R.color.title));
        tv_yuanchuang.setGravity(Gravity.CENTER);
        tv_yuanchuang.setOnClickListener(this);
        LinearLayout.LayoutParams lparams2 = new LinearLayout.LayoutParams(
                0, DisplayUtils.dip2px(getActivity(), 35));
        lparams2.weight = 1;
        home_ll_top.addView(tv_yuanchuang, lparams2);

        tv_gonglve = new TextView(getActivity());
        tv_gonglve.setText("攻略");
        tv_gonglve.setTextColor(getResources().getColor(R.color.title));
        tv_gonglve.setGravity(Gravity.CENTER);
        tv_gonglve.setOnClickListener(this);
        LinearLayout.LayoutParams lparams3 = new LinearLayout.LayoutParams(
                0, DisplayUtils.dip2px(getActivity(), 35));
        lparams3.weight = 1;
        home_ll_top.addView(tv_gonglve, lparams3);

        tv_guanzhu = new TextView(getActivity());
        tv_guanzhu.setText("关注");
        tv_guanzhu.setTextColor(getResources().getColor(R.color.title));
        tv_guanzhu.setGravity(Gravity.CENTER);
        tv_guanzhu.setOnClickListener(this);
        LinearLayout.LayoutParams lparams4 = new LinearLayout.LayoutParams(
                0, DisplayUtils.dip2px(getActivity(), 35));
        lparams4.weight = 1;
        home_ll_top.addView(tv_guanzhu, lparams4);
    }

    public class FragmentAdapter extends FragmentPagerAdapter{

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

        @Override
        public Fragment getItem(int i) {
            if (i == 0) {
                return new News1Fragment();
            } else if (i == 1) {
                return new News2Fragment();
            } else if (i == 2) {
                return new News3Fragment();
            } else {
                return new News4Fragment();
            }
        }

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

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_zixun) {
            home_vp_content.setCurrentItem(0);
        } else if (v == tv_yuanchuang) {
            home_vp_content.setCurrentItem(1);
        } else if (v == tv_gonglve) {
            home_vp_content.setCurrentItem(2);
        } else if (v == tv_guanzhu) {
            home_vp_content.setCurrentItem(3);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
                               int positionOffsetPixels) {
        if (positionOffset != 0) {
            lparams.leftMargin = (int) (width * (position + positionOffset) + width / 4);
            home_slide_line.setLayoutParams(lparams);
        } else {
            if (currentItem != home_vp_content.getCurrentItem()) {
                currentItem = home_vp_content.getCurrentItem();
                EventBus.getDefault().post(new EBUISwitch("NewsFragment", currentItem));
            }
        }
    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            tv_zixun.setTextColor(getResources().getColor(R.color.theme));
            tv_yuanchuang.setTextColor(getResources().getColor(R.color.title));
            tv_gonglve.setTextColor(getResources().getColor(R.color.title));
            tv_guanzhu.setTextColor(getResources().getColor(R.color.title));
        } else if (position == 1) {
            tv_zixun.setTextColor(getResources().getColor(R.color.title));
            tv_yuanchuang.setTextColor(getResources().getColor(R.color.theme));
            tv_gonglve.setTextColor(getResources().getColor(R.color.title));
            tv_guanzhu.setTextColor(getResources().getColor(R.color.title));
        } else if (position == 2) {
            tv_zixun.setTextColor(getResources().getColor(R.color.title));
            tv_yuanchuang.setTextColor(getResources().getColor(R.color.title));
            tv_gonglve.setTextColor(getResources().getColor(R.color.theme));
            tv_guanzhu.setTextColor(getResources().getColor(R.color.title));
        }else if (position == 3){
            tv_zixun.setTextColor(getResources().getColor(R.color.title));
            tv_yuanchuang.setTextColor(getResources().getColor(R.color.title));
            tv_gonglve.setTextColor(getResources().getColor(R.color.title));
            tv_guanzhu.setTextColor(getResources().getColor(R.color.theme));
        }

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    public void onEventMainThread(EBUISwitch busNine) {
        if ("MainActivity".equals(busNine.getFrom())) {
            if (busNine.getPosition() == 1) {
                EventBus.getDefault().post(
                        new EBUISwitch("NewsFragment", home_vp_content.getCurrentItem()));
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        tv_zixun = null;
        tv_yuanchuang = null;
        tv_gonglve = null;
        tv_guanzhu = null;
    }
}

