package com.smartcar.pvrmemoryleakdemo;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

/**
 * Created by tongguangli on 17-11-15.
 */

public class TabController implements View.OnClickListener{

    private View mTab1 = null;
    private View mTab2 = null;
    private View mTab3 = null;
    private View mTab4 = null;
    private View mTab5 = null;
    private ViewFlipper mViewFlipper = null;
    private Context mContext = null;
    private View mView = null;

    public TabController(Context context, View mView) {
        mContext = context;
        this.mView = mView;
        mTab1 = findViewById(R.id.tab1);
        mTab1.setSelected(true);
        mTab2 = findViewById(R.id.tab2);
        mTab3 = findViewById(R.id.tab3);
        mTab4 = findViewById(R.id.tab4);
        mTab5 = findViewById(R.id.tab5);
        mViewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
        mTab1.setOnClickListener(this);
        mTab2.setOnClickListener(this);
        mTab3.setOnClickListener(this);
        mTab4.setOnClickListener(this);
        mTab5.setOnClickListener(this);
    }

    private View findViewById(int id) {
        return this.mView.findViewById(id);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tab1:
                displayChild(0);
                break;
            case R.id.tab2:
                displayChild(1);
                break;
            case R.id.tab3:
                displayChild(2);
                break;
            case R.id.tab4:
                displayChild(3);
                break;
            case R.id.tab5:
                displayChild(4);
                break;
            default:
                break;
        }
    }

    private void displayChild(int position) {
        int curPostion = mViewFlipper.getDisplayedChild();
        if (position > curPostion) {
            mViewFlipper.setOutAnimation(createPageLeftOutAnimation());
            mViewFlipper.setInAnimation(createPageRightInAnimation());
        } else if (position < curPostion) {
            mViewFlipper.setOutAnimation(createPageRightOutAnimation());
            mViewFlipper.setInAnimation(createPageLeftInAnimation());
        }
        if (position != curPostion) {
            mViewFlipper.clearDisappearingChildren();
            mViewFlipper.clearAnimation();
            mViewFlipper.clearChildFocus(mViewFlipper.getCurrentView());
            mViewFlipper.setDisplayedChild(position);
        }
        switch (position) {
            case 0:
                mTab1.setSelected(true);
                mTab2.setSelected(false);
                mTab3.setSelected(false);
                mTab4.setSelected(false);
                mTab5.setSelected(false);
                break;
            case 1:
                mTab1.setSelected(false);
                mTab2.setSelected(true);
                mTab3.setSelected(false);
                mTab4.setSelected(false);
                mTab5.setSelected(false);
                break;
            case 2:
                mTab1.setSelected(false);
                mTab2.setSelected(false);
                mTab3.setSelected(true);
                mTab4.setSelected(false);
                mTab5.setSelected(false);
                break;
            case 3:
                mTab1.setSelected(false);
                mTab2.setSelected(false);
                mTab3.setSelected(false);
                mTab4.setSelected(true);
                mTab5.setSelected(false);
                break;
            case 4:
                mTab1.setSelected(false);
                mTab2.setSelected(false);
                mTab3.setSelected(false);
                mTab4.setSelected(false);
                mTab5.setSelected(true);
                break;
        }
    }

    private Animation createPageLeftOutAnimation() {
        return AnimationUtils.loadAnimation(mContext, R.anim.sc_ctrl_left_out_anim);
    }

    private Animation createPageRightInAnimation() {
        return AnimationUtils.loadAnimation(mContext, R.anim.sc_ctrl_right_in_anim);
    }

    private Animation createPageLeftInAnimation() {
        return AnimationUtils.loadAnimation(mContext, R.anim.sc_ctrl_left_in_anim);
    }

    private Animation createPageRightOutAnimation() {
        return AnimationUtils.loadAnimation(mContext, R.anim.sc_ctrl_right_out_anim);
    }
}
