package com.itant.joycity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.itant.joycity.activity.BaseActivity;
import com.itant.joycity.adapter.FragmentAdapter;
import com.itant.joycity.adapter.NoScrollViewPager;
import com.itant.joycity.fragment.FirstFragment;
import com.itant.joycity.fragment.FourthFragment;
import com.itant.joycity.fragment.SecondFragment;
import com.itant.joycity.fragment.ThirdFragment;
import com.itant.joycity.tool.HttpTool;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONObject;
import org.xutils.x;

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

import cz.msebera.android.httpclient.Header;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private NoScrollViewPager vp_main;
    private FragmentAdapter mAdapter;
    private List<Fragment> mFragments;

    private RadioGroup rg_bottom;
    private LinearLayout ll_first;
    private ImageView iv_first;
    private TextView tv_first;

    private LinearLayout ll_second;
    private ImageView iv_second;
    private TextView tv_second;

    private LinearLayout ll_third;
    private ImageView iv_third;
    private TextView tv_third;

    private LinearLayout ll_fourth;
    private ImageView iv_fourth;
    private TextView tv_fourth;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        ll_first = (LinearLayout) findViewById(R.id.ll_first);
        ll_first.setOnClickListener(this);
        ll_second = (LinearLayout) findViewById(R.id.ll_second);
        ll_second.setOnClickListener(this);
        ll_third = (LinearLayout) findViewById(R.id.ll_third);
        ll_third.setOnClickListener(this);
        ll_fourth = (LinearLayout) findViewById(R.id.ll_fourth);
        ll_fourth.setOnClickListener(this);

        iv_first = (ImageView) findViewById(R.id.iv_first);
        iv_second = (ImageView) findViewById(R.id.iv_second);
        iv_third = (ImageView) findViewById(R.id.iv_third);
        iv_fourth = (ImageView) findViewById(R.id.iv_fourth);

        tv_first = (TextView) findViewById(R.id.tv_first);
        tv_second = (TextView) findViewById(R.id.tv_second);
        tv_third = (TextView) findViewById(R.id.tv_third);
        tv_fourth = (TextView) findViewById(R.id.tv_fourth);

        vp_main = (NoScrollViewPager) findViewById(R.id.vp_main);

        mFragments = new ArrayList<>();
        mFragments.add(new FirstFragment());
        mFragments.add(new SecondFragment());
        mFragments.add(new ThirdFragment());
        mFragments.add(new FourthFragment());

        mAdapter = new FragmentAdapter(getSupportFragmentManager(), mFragments);
        vp_main.setAdapter(mAdapter);
        vp_main.setOffscreenPageLimit(4);
        vp_main.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        onFirstSelected();
                        break;

                    case 1:
                        onSecondSelected();
                        break;

                    case 2:
                        onThirdSelected();
                        break;

                    case 3:
                        onFourthSelected();
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        /*HttpTool.getHttpClient().get("url", new RequestParams(), new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
            }
        });*/
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_first:
                vp_main.setCurrentItem(0);
                break;

            case R.id.ll_second:
                vp_main.setCurrentItem(1);
                break;

            case R.id.ll_third:
                vp_main.setCurrentItem(2);
                break;

            case R.id.ll_fourth:
                vp_main.setCurrentItem(3);
                break;

            default:
                break;
        }
    }

    private void onFirstSelected() {
        iv_first.setImageResource(R.mipmap.news_selected);
        iv_second.setImageResource(R.mipmap.enter_normal);
        iv_third.setImageResource(R.mipmap.backup_normal);
        iv_fourth.setImageResource(R.mipmap.money_normal);

        tv_first.setTextColor(Color.parseColor("#ec971f"));
        tv_second.setTextColor(getResources().getColor(R.color.font));
        tv_third.setTextColor(getResources().getColor(R.color.font));
        tv_fourth.setTextColor(getResources().getColor(R.color.font));
    }

    private void onSecondSelected() {
        iv_first.setImageResource(R.mipmap.news_normal);
        iv_second.setImageResource(R.mipmap.enter_selected);
        iv_third.setImageResource(R.mipmap.backup_normal);
        iv_fourth.setImageResource(R.mipmap.money_normal);

        tv_first.setTextColor(getResources().getColor(R.color.font));
        tv_second.setTextColor(Color.parseColor("#ec971f"));
        tv_third.setTextColor(getResources().getColor(R.color.font));
        tv_fourth.setTextColor(getResources().getColor(R.color.font));

    }

    private void onThirdSelected() {
        iv_first.setImageResource(R.mipmap.news_normal);
        iv_second.setImageResource(R.mipmap.enter_normal);
        iv_third.setImageResource(R.mipmap.backup_selected);
        iv_fourth.setImageResource(R.mipmap.money_normal);

        tv_first.setTextColor(getResources().getColor(R.color.font));
        tv_second.setTextColor(getResources().getColor(R.color.font));
        tv_third.setTextColor(Color.parseColor("#ec971f"));
        tv_fourth.setTextColor(getResources().getColor(R.color.font));
    }

    private void onFourthSelected() {
        iv_first.setImageResource(R.mipmap.news_normal);
        iv_second.setImageResource(R.mipmap.enter_normal);
        iv_third.setImageResource(R.mipmap.backup_normal);
        iv_fourth.setImageResource(R.mipmap.money_selected);

        tv_first.setTextColor(getResources().getColor(R.color.font));
        tv_second.setTextColor(getResources().getColor(R.color.font));
        tv_third.setTextColor(getResources().getColor(R.color.font));
        tv_fourth.setTextColor(Color.parseColor("#ec971f"));
    }
}
