package com.itant.shiwushu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;

import com.itant.shiwushu.adapter.FragmentPagerItemAdapter;
import com.itant.shiwushu.base.BaseSkinActivity;
import com.itant.shiwushu.bean.User;
import com.itant.shiwushu.manager.CityManager;
import com.itant.shiwushu.manager.ToastManager;
import com.itant.shiwushu.manager.UserManager;
import com.itant.shiwushu.tool.BottomNavigationViewHelper;
import com.itant.shiwushu.ui.menu.found.FoundFragment;
import com.itant.shiwushu.ui.menu.lost.LostFragment;
import com.itant.shiwushu.ui.menu.me.MeFragment;
import com.itant.shiwushu.ui.menu.message.MessageFragment;

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

public class MainActivity extends BaseSkinActivity {
    public static final int CODE_CITY_LOST = 1;
    public static final int CODE_CITY_FOUND = 2;
    public static final int CODE_CITY_PUBLISH = 3;
    public static final String KEY_CITY_TYPE = "city_type";

    private ViewPager vp_main;
    private List<Fragment> fragmentList;
    private LostFragment lostFragment;
    private FoundFragment foundFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 去除底部动画效果
        BottomNavigationView navigation = findViewById(R.id.navigation);
        BottomNavigationViewHelper.disableShiftMode(navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        fragmentList = new ArrayList<>();
        lostFragment = new LostFragment();
        foundFragment = new FoundFragment();
        fragmentList.add(lostFragment);
        fragmentList.add(foundFragment);
        fragmentList.add(new MessageFragment());
        fragmentList.add(new MeFragment());
        vp_main = findViewById(R.id.vp_main);
        vp_main.setOffscreenPageLimit(fragmentList.size());

        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(getSupportFragmentManager(), fragmentList);
        vp_main.setAdapter(adapter);
        vp_main.setCurrentItem(0);

        /*if (UserManager.getInstance().getUser().getUserType() == User.USER_TYPE_VIP) {
            ToastManager.getInstance().toastShort("欢迎VIP用户");
        }*/
        if (UserManager.getInstance().getUser().getUserType() == User.USER_TYPE_INVALID) {
            // 非法用户，可能是诈骗号
            ToastManager.getInstance().toastShort("您的账户有误");
            finish();
            return;
        }
        CityManager.getInstance().initCity();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_lost:
                    // 选中失物
                    vp_main.setCurrentItem(0);
                    return true;
                case R.id.menu_found:
                    // 选中招领
                    vp_main.setCurrentItem(1);
                    return true;

                case R.id.menu_message:
                    // 选中消息
                    vp_main.setCurrentItem(2);
                    return true;
                case R.id.menu_me:
                    // 选中我的
                    vp_main.setCurrentItem(3);
                    return true;
            }
            return false;
        }
    };

    private long lastBackMillis;
    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - lastBackMillis > 2500) {
            lastBackMillis = System.currentTimeMillis();
            ToastManager.getInstance().toastShort("再按一次退出");
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }

        if (requestCode == CODE_CITY_LOST) {
            lostFragment.onCityChoose();
        }

        if (requestCode == CODE_CITY_FOUND) {
            foundFragment.onCityChoose();
        }
    }
}
