package com.android.chmo.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.ui.fragment.BrokerFragment;
import com.android.chmo.ui.fragment.FindFragment;
import com.android.chmo.ui.fragment.HomeFragment;
import com.android.chmo.ui.fragment.MeFragment;
import com.android.chmo.ui.fragment.MessageFragment;
import com.android.chmo.ui.view.NoSlideViewPager;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.msg.MsgService;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {
    private final int FRAGMENT_HOME = 0;
    private final int FRAGMENT_BROKER = 1;
    private final int FRAGMENT_FIND = 2;
    private final int FRAGMENT_MESSAGE = 3;
    private final int FRAGMENT_ME = 4;

    @BindView(R.id.pager)
    NoSlideViewPager mPager;

    @BindView(R.id.tab_home)
    View tabHome;

    @BindView(R.id.tab_broker)
    View tabBroker;

    @BindView(R.id.tab_message)
    View tabMessage;

    @BindView(R.id.tab_me)
    View tabMe;

    @BindView(R.id.find)
    View findBtn;

    @BindView(R.id.unread)
    TextView unreadView;

    private View curTab;

    private int curFragment;


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

    @Override
    public void initView() {
        mPager.setOffscreenPageLimit(5);
        mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        curTab = tabHome;
        curTab.setSelected(true);

        registerObserver(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        fillUnreadView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        registerObserver(false);
    }

    @OnClick({R.id.tab_home, R.id.tab_broker, R.id.tab_message, R.id.tab_me, R.id.find})
    public void onTabClick(View v) {
        if (null != curTab) {
            curTab.setSelected(false);
        }
        curTab = v;
        curTab.setSelected(true);
        switch (v.getId()) {
            case R.id.tab_home:
                curFragment = FRAGMENT_HOME;
                break;
            case R.id.tab_broker:
                curFragment = FRAGMENT_BROKER;
                break;
            case R.id.tab_message:
                curFragment = FRAGMENT_MESSAGE;
                break;
            case R.id.tab_me:
                curFragment = FRAGMENT_ME;
                break;
            case R.id.find:
                curFragment = FRAGMENT_FIND;
                break;
            default:
                break;
        }
        mPager.setCurrentItem(curFragment, false);
    }

    private void fillUnreadView() {
        int count =  NIMClient.getService(MsgService.class).getTotalUnreadCount();
        if (count == 0) {
            unreadView.setText("0");
            unreadView.setVisibility(View.GONE);
        }else {
            unreadView.setVisibility(View.VISIBLE);
            unreadView.setText(count > 9 ? "9+" : String.valueOf(count));
        }
    }

    private void registerObserver(boolean register) {
        if (register) {
            registerReceiver(msgCountReceiver,new IntentFilter("com.android.chmo.REFRESH_MSG"));
        }else {
            unregisterReceiver(msgCountReceiver);
        }
    }

    private BroadcastReceiver msgCountReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            fillUnreadView();
        }
    };

    private class MyPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case FRAGMENT_HOME:
                    return Fragment.instantiate(getApplicationContext(),
                            HomeFragment.class.getName());
                case FRAGMENT_BROKER:
                    return Fragment.instantiate(getApplicationContext(),
                            BrokerFragment.class.getName());
                case FRAGMENT_FIND:
                    return Fragment.instantiate(getApplicationContext(),
                            FindFragment.class.getName());
                case FRAGMENT_MESSAGE:
                    return Fragment.instantiate(getApplicationContext(),
                            MessageFragment.class.getName());
                case FRAGMENT_ME:
                    return Fragment.instantiate(getApplicationContext(),
                            MeFragment.class.getName());
            }

            throw new RuntimeException("undefine position:" + position);
        }

        @Override
        public int getCount() {
            return 5;
        }
    }


    private long backPressTime = 0;
    @Override
    public void onBackPressed() {
        long time = System.currentTimeMillis();
        if((time-backPressTime) > 2000) {
            backPressTime = time;
            showToast("再按一次退出应用");
            return;
        }

        super.onBackPressed();
    }
}
