package com.android.chmo.ui.activity.order;

import android.content.Intent;
import android.os.Bundle;
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.view.View;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.ui.activity.MainActivity;
import com.android.chmo.ui.fragment.order.OrderFragment;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/4.
 */

public class MyOrderActivity extends BaseActivity {
    private final String[] orderState = new String[]{"all","1","2","3"};
    @BindView(R.id.all)
    View tabAll;
    @BindView(R.id.noPay)
    View tabNoPay;
    @BindView(R.id.working)
    View tabWorking;
    @BindView(R.id.end)
    View tabEnd;
    @BindView(R.id.pager)
    ViewPager pager;

    private OrderPagerAdapter pagerAdapter;

    private View curTab;

    private int curPage;


    @OnClick({R.id.all,R.id.noPay,R.id.working,R.id.end})
    public void onTabClick(View v) {
        switch (v.getId()) {
            case R.id.all:
                curPage = 0;
                break;
            case R.id.noPay:
                curPage = 1;
                break;
            case R.id.working:
                curPage = 2;
                break;
            case R.id.end:
                curPage = 3;
                break;
        }
        pager.setCurrentItem(curPage,true);
        changeTab();
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_my_order;
    }

    @Override
    public void initView() {
        pagerAdapter = new OrderPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(pagerAdapter);
        pager.setOffscreenPageLimit(4);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                curPage = position;
                changeTab();
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        changeTab();
    }

    private void changeTab() {
        if(null != curTab) {
            curTab.setSelected(false);
        }
        switch (curPage) {
            case 0:
                curTab = tabAll;
                break;
            case 1:
                curTab = tabNoPay;
                break;
            case 2:
                curTab = tabWorking;
                break;
            case 3:
                curTab = tabEnd;
                break;
        }

        curTab.setSelected(true);
    }


    @Override
    public void finish() {
        if(getIntent().hasExtra("from")) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            openPage(intent);
        }
        super.finish();
    }

    private class OrderPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            String state = orderState[position];
            OrderFragment fragment = (OrderFragment) Fragment.instantiate(getContext(), OrderFragment.class.getName());
            Bundle args = new Bundle();
            args.putString("state",state);
            fragment.setArguments(args);

            return fragment;
        }

        @Override
        public int getCount() {
            return orderState.length;
        }

        @Override
        public long getItemId(int position) {
            int hashCode = orderState[position].hashCode();
            return hashCode;
        }
    }
}
