package com.qhclh.ytzh.index.shangpin;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.work.carsmannage.BaseActivity1;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 青花瓷 on 2017/12/28.
 */

public class InHouseActivity extends BaseActivity1 {
    @BindView(R.id.toolbar1)
    Toolbar mToolbar;

    @BindView(R.id.iv_inhouse_all)
    ImageView iv_inhouse_all;
    @BindView(R.id.tv_inhouse_all)
    TextView tv_inhouse_all;

    @BindView(R.id.iv_inhouse_no)
    ImageView iv_inhouse_no;
    @BindView(R.id.tv_inhouse_no)
    TextView tv_inhouse_no;

    @BindView(R.id.iv_inhouse_yes)
    ImageView iv_inhouse_yes;
    @BindView(R.id.tv_inhouse_yes)
    TextView tv_inhouse_yes;

    @BindView(R.id.ll_inhouse_all)
    LinearLayout ll_inhouse_all;
    @BindView(R.id.ll_inhouse_no)
    LinearLayout ll_inhouse_no;
    @BindView(R.id.ll_inhouse_yes)
    LinearLayout ll_inhouse_yes;

    ///< all
    private static final int TAB_POSITION_ALL = 0;
    ///< no
    private static final int TAB_POSITION_NO = 1;
    ///< yes
    private static final int TAB_POSITION_YES = 2;
    private int mCurrentTabPosition = -1;
    private int mTargetTabPosition = TAB_POSITION_ALL;

    private InHouseAllFragment inHouseAllFragment;
    private InHouseNoFragment inHouseNoFragment;
    private InHouseYesFragment inHouseYesFragment;

    @Override
    protected int setLayoutId() {
        return R.layout.act_inhouse;
    }

    @Override
    protected void initView() {
        initToolbar(mToolbar, "入舍单报表", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ///< 点击切换fragment
        onTabClick(mTargetTabPosition);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initOper() {

    }

    @OnClick({R.id.ll_inhouse_all, R.id.ll_inhouse_no, R.id.ll_inhouse_yes})
    public void onClick(View view) {
        switch (view.getId()) {
            ///< all
            case R.id.ll_inhouse_all: {
                onTabClick(TAB_POSITION_ALL);
                break;
            }
            ///< no
            case R.id.ll_inhouse_no: {
                onTabClick(TAB_POSITION_NO);
                break;
            }

            ///< yes
            case R.id.ll_inhouse_yes: {
                onTabClick(TAB_POSITION_YES);
                break;
            }
        }
    }

    private void onTabClick(int tabPosition) {
        if (mCurrentTabPosition == tabPosition) {
            return;
        }
        mCurrentTabPosition = tabPosition;
        changeTabState(tabPosition);
        changeFragment(tabPosition);
    }

    private void changeTabState(int tabPosition) {
        clearAllTabState();
        switch (tabPosition) {
            case TAB_POSITION_ALL: {
                iv_inhouse_all.setImageResource(R.drawable.quanbu);
                tv_inhouse_all.setTextColor(getResources().getColor(R.color.white));
                ll_inhouse_all.setBackgroundResource(R.drawable.shape_kuang2);
                break;
            }

            case TAB_POSITION_NO: {
                iv_inhouse_no.setImageResource(R.drawable.weishen);
                tv_inhouse_no.setTextColor(getResources().getColor(R.color.white));
                ll_inhouse_no.setBackgroundResource(R.drawable.shape_kuang2);
                break;
            }
            case TAB_POSITION_YES: {
                iv_inhouse_yes.setImageResource(R.drawable.yishen1);
                tv_inhouse_yes.setTextColor(getResources().getColor(R.color.white));
                ll_inhouse_yes.setBackgroundResource(R.drawable.shape_kuang2);
                break;
            }

        }
    }

    private void clearAllTabState() {
        ///< all
        iv_inhouse_all.setImageResource(R.drawable.quanbu1);
        tv_inhouse_all.setTextColor(getResources().getColor(R.color.grey_767c82));
        ll_inhouse_all.setBackgroundResource(R.drawable.shape_kuang1);
        ///< no
        iv_inhouse_no.setImageResource(R.drawable.weishen1);
        tv_inhouse_no.setTextColor(getResources().getColor(R.color.grey_767c82));
        ll_inhouse_no.setBackgroundResource(R.drawable.shape_kuang1);
        ///< yes
        iv_inhouse_yes.setImageResource(R.drawable.yishen);
        tv_inhouse_yes.setTextColor(getResources().getColor(R.color.grey_767c82));
        ll_inhouse_yes.setBackgroundResource(R.drawable.shape_kuang1);
    }


    private void changeFragment(int tabPosition) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragment(transaction);
        switch (tabPosition) {
            case TAB_POSITION_ALL: {
                if (inHouseAllFragment == null) {
                    inHouseAllFragment = new InHouseAllFragment();
                    transaction.add(R.id.inhouse_container, inHouseAllFragment);
                } else {
                    transaction.show(inHouseAllFragment);
                }
                break;
            }

            case TAB_POSITION_NO: {
                if (inHouseNoFragment == null) {
                    inHouseNoFragment = new InHouseNoFragment();
                    transaction.add(R.id.inhouse_container, inHouseNoFragment);
                } else {
                    transaction.show(inHouseNoFragment);
                }
                break;
            }

            case TAB_POSITION_YES: {
                if (inHouseYesFragment == null) {
                    inHouseYesFragment = new InHouseYesFragment();
                    transaction.add(R.id.inhouse_container, inHouseYesFragment);
                } else {
                    transaction.show(inHouseYesFragment);
                }
                break;
            }

        }
        transaction.commitAllowingStateLoss();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (inHouseAllFragment != null) {
            transaction.hide(inHouseAllFragment);
        }

        if (inHouseNoFragment != null) {
            transaction.hide(inHouseNoFragment);
        }
        if (inHouseYesFragment != null) {
            transaction.hide(inHouseYesFragment);
        }
    }
}
