package com.qhclh.ytzh.work.carsmannage;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.ui.CalendarDialogFragment;
import com.qhclh.ytzh.utils.DateTimeUtil;

import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 青花瓷 on 2017/11/30.
 */
///< 父母代车辆管理雏苗
public class FumudaiCarsManageActivity extends BaseActivity1 implements CalendarDialogFragment.CalendarClickListener{
    @BindView(R.id.toolbar1)
    Toolbar mToolbar;
    @BindView(R.id.chicks)
    TextView chicks;
    @BindView(R.id.GrossChicken)
    TextView GrossChicken;
    @BindView(R.id.feed)
    TextView feed;

    ///< 鸡苗
    private static final int TAB_POSITION_CHICKS = 0;
    ///< 毛鸡
    private static final int TAB_POSITION_GROSSCHICKEN = 1;
    ///< 饲料
    private static final int TAB_POSITION_FEED = 2;
    private int mCurrentTabPosition = -1;
    private int mTargetTabPosition = TAB_POSITION_CHICKS;

    private ChicksFragment chicksFragment;
    private GrossChickenFragment grossChickenFragment;
    private FeedFragment feedFragment;

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

    @Override
    protected void initView() {
        ///< 点击切换fragment
        onTabClick(mTargetTabPosition);
        initToolbar(mToolbar, "车辆管理", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        feed.setVisibility(View.GONE);
        GrossChicken.setVisibility(View.GONE);
    }

    @Override
    protected void initOper() {

    }

    @OnClick({R.id.chicks,R.id.GrossChicken,R.id.feed})
    public void OnClick(View v){
        switch (v.getId()){
            case R.id.chicks:
                onTabClick(TAB_POSITION_CHICKS);
                break;
            case R.id.GrossChicken:
                onTabClick(TAB_POSITION_GROSSCHICKEN);
                break;
            case R.id.feed:
                onTabClick(TAB_POSITION_FEED);
                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_CHICKS: {
                chicks.setTextColor(this.getResources().getColor(R.color.white));
                chicks.setBackgroundColor(this.getResources().getColor(R.color.green02aa3e));
                break;
            }

            case TAB_POSITION_GROSSCHICKEN: {
                GrossChicken.setTextColor(this.getResources().getColor(R.color.white));
                GrossChicken.setBackgroundColor(this.getResources().getColor(R.color.green02aa3e));
                break;
            }
            case TAB_POSITION_FEED: {
                feed.setTextColor(this.getResources().getColor(R.color.white));
                feed.setBackgroundColor(this.getResources().getColor(R.color.green02aa3e));
                break;
            }
        }
    }


    private void clearAllTabState() {
        ///< 首页
        chicks.setTextColor(this.getResources().getColor(R.color.grey666666));
        chicks.setBackgroundColor(this.getResources().getColor(R.color.white));
        ///< 工作
        GrossChicken.setTextColor(this.getResources().getColor(R.color.grey666666));
        GrossChicken.setBackgroundColor(this.getResources().getColor(R.color.white));
        ///< 我的
        feed.setTextColor(this.getResources().getColor(R.color.grey666666));
        feed.setBackgroundColor(this.getResources().getColor(R.color.white));

    }


    private void changeFragment(int tabPosition) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragment(transaction);
        switch (tabPosition) {
            case TAB_POSITION_CHICKS: {
                if (chicksFragment == null) {
                    chicksFragment = new ChicksFragment();
                    transaction.add(R.id.main_container, chicksFragment);
                } else {
                    transaction.show(chicksFragment);
                }
                break;
            }

            case TAB_POSITION_GROSSCHICKEN: {
                if (grossChickenFragment == null) {
                    grossChickenFragment = new GrossChickenFragment();
                    transaction.add(R.id.main_container, grossChickenFragment);
                } else {
                    transaction.show(grossChickenFragment);
                }
                break;
            }

            case TAB_POSITION_FEED: {
                if (feedFragment == null) {
                    feedFragment = new FeedFragment();
                    transaction.add(R.id.main_container, feedFragment);
                } else {
                    transaction.show(feedFragment);
                }
                break;
            }
        }
        transaction.commitAllowingStateLoss();
    }

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

        if (grossChickenFragment != null) {
            transaction.hide(grossChickenFragment);
        }
        if (feedFragment != null) {
            transaction.hide(feedFragment);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.screen, menu);
        menu.findItem(R.id.screenr).setVisible(true);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.screenr:
                    new CalendarDialogFragment().show(getSupportFragmentManager(), "start");
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onStartCalendarClick(Date date) {
        showToast(DateTimeUtil.FormatDate(date));
    }

    @Override
    public void onEndCalendarClick(Date date) {
        showToast(DateTimeUtil.FormatDate(date));
    }
}
