package com.cooleshow.teacher.ui.main;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.common.im.ui.MessageFragment;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.LogUtils;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.HomePageAdapter;
import com.cooleshow.teacher.bean.TeacherUserInfo;
import com.cooleshow.teacher.contract.MainContract;
import com.cooleshow.teacher.databinding.ActivityMainBinding;
import com.cooleshow.teacher.presenter.main.MainPresenter;
import com.cooleshow.usercenter.bean.UserInfo;
import com.cooleshow.usercenter.helper.UserHelper;
import com.daya.live_teaching.common.ResultCallback;
import com.daya.live_teaching.im.IMManager;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import io.rong.imlib.RongIMClient;

/**
 * Author by pq, Date on 2022/4/20.
 */
@Route(path = RouterPath.APPCenter.PATH_HOME)
public class MainActivity extends BaseMVPActivity<ActivityMainBinding, MainPresenter> implements MainContract.MainView {
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private HomeFragment mHomeFragment;
    private MineFragment mMineFragment;
    private MallFragment shopMallFragment;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
    }

    @NonNull
    @Override
    protected ActivityMainBinding getLayoutView() {
        return ActivityMainBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        HomePageAdapter homePageAdapter = new HomePageAdapter(this);
        mHomeFragment = new HomeFragment();
        CourseTableFragment courseTableFragment = new CourseTableFragment();
        MessageFragment messageFragment = new MessageFragment();
        shopMallFragment = new MallFragment();
        mMineFragment = new MineFragment();
        mFragments.add(mHomeFragment);
        mFragments.add(courseTableFragment);
        mFragments.add(messageFragment);
        mFragments.add(shopMallFragment);
        mFragments.add(mMineFragment);
        homePageAdapter.setFragments(mFragments);
        getViewBinding().viewPager.setAdapter(homePageAdapter);
        getViewBinding().viewPager.setOffscreenPageLimit(mFragments.size());
        getViewBinding().viewPager.setUserInputEnabled(false);
        getViewBinding().navigation.setItemIconTintList(null);
        getViewBinding().navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                return onTabClick(item.getItemId());
            }
        });
    }


    @Override
    public void onBackPressed() {
        int currentItem = getViewBinding().viewPager.getCurrentItem();
        if (currentItem == 3) {
            //商城
            if (null != shopMallFragment) {
                shopMallFragment.clickBack();
            }
        } else {
            super.onBackPressed();
        }
    }

    public void clickBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void initData() {
        super.initData();
        connectIM();
    }


    /**
     * 连接rong IM
     */
    private void connectIM() {
        if (UserHelper.isLogin()) {
            String userIMToken = UserHelper.getUserIMToken();
            if (!TextUtils.isEmpty(userIMToken)) {
                RongIMClient.ConnectionStatusListener.ConnectionStatus currentConnectionStatus = RongIMClient.getInstance().getCurrentConnectionStatus();
                if (currentConnectionStatus != RongIMClient.ConnectionStatusListener.ConnectionStatus.CONNECTED) {
                    IMManager.getInstance().login(userIMToken, new ResultCallback<String>() {
                        @Override
                        public void onSuccess(String s) {
                            LogUtils.i("im connect success:" + s);
                        }

                        @Override
                        public void onFail(int errorCode, String errorStr) {
                            LogUtils.i("im connect fail:" + errorStr + "-errorCode:" + errorCode);
                        }
                    });
                }
            }
        }
    }
    /*
    上一次的tab位置
     */
    private int lastCurrentItemPosition = 0;

    private boolean onTabClick(int viewId) {
        if (viewId == R.id.menu_home) {
            if (lastCurrentItemPosition!=0){
                presenter.getTeacherUserInfo();
            }
            lastCurrentItemPosition=0;
            getViewBinding().viewPager.setCurrentItem(0, false);
            return true;
        }
        if (viewId == R.id.menu_coursetable) {
            lastCurrentItemPosition=1;
            getViewBinding().viewPager.setCurrentItem(1, false);
            return true;
        }
        if (viewId == R.id.menu_chat) {
            lastCurrentItemPosition=2;
            getViewBinding().viewPager.setCurrentItem(2, false);
            return true;
        }
        if (viewId == R.id.menu_mall) {
            lastCurrentItemPosition=3;
            getViewBinding().viewPager.setCurrentItem(3, false);
            return true;
        }
        if (viewId == R.id.menu_mine) {
            if (lastCurrentItemPosition!=4){
                presenter.getTeacherUserInfo();
            }
            lastCurrentItemPosition=4;
            getViewBinding().viewPager.setCurrentItem(4, false);
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.getUserInfo();
        presenter.getTeacherUserInfo();
    }

    @Override
    public void getUserInfoSuccess(UserInfo userInfo) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        //存储userInfo信息
        UserHelper.saveUserInfo(userInfo);

    }

    @Override
    public void getTeacherInfoSuccess(TeacherUserInfo teacherUserInfo) {
        if (isFinishing() || isDestroyed()) {
            return;
        }
        if (mHomeFragment != null) {
            mHomeFragment.getTeacherInfoSuccess(teacherUserInfo);
        }
        if (mMineFragment != null) {
            mMineFragment.getTeacherInfoSuccess(teacherUserInfo);
        }
    }

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }
}
