package com.wisewoods.eduandroid.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.Utils;
import com.pgyer.pgyersdk.PgyerSDKManager;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.liteav.meeting.model.TRTCMeeting;
import com.wisewoods.base.app.UserManager;
import com.wisewoods.base.base.BaseActivity;
import com.wisewoods.base.bean.BaseEvent;
import com.wisewoods.base.bean.LoginUser;
import com.wisewoods.base.bean.MsgCount;
import com.wisewoods.base.bean.Role;
import com.wisewoods.base.bean.VersionInfo;
import com.wisewoods.base.net.ApiService;
import com.wisewoods.base.net.JCallback;
import com.wisewoods.base.net.JDCallback;
import com.wisewoods.base.utils.ImgUtils;
import com.wisewoods.eduandroid.R;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.app.ImgManager;
import com.wisewoods.eduandroid.databinding.ActivityMainBinding;
import com.wisewoods.eduandroid.ui.adapter.MainTab;
import com.wisewoods.eduandroid.ui.adapter.MainTabAdapter;
import com.wisewoods.eduandroid.view.AppUpdateUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import retrofit2.Response;

public class MainActivity extends BaseActivity<ActivityMainBinding> implements SwipeRefreshLayout.OnRefreshListener {

    private MainTabAdapter mAdapter;

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void logout(BaseEvent.LogoutEvent event) {
        TRTCMeeting.sharedInstance(Utils.getApp()).logout(null);

        ActivityUtils.finishAllActivities();
        XGPushManager.clearAccounts(Utils.getApp());
        Intent intent = new Intent(Utils.getApp(), LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ActivityUtils.startActivity(intent);
    }

    private void getVersionData() {
        App.api.getAppInfo(ApiService.APPID).enqueue(new JCallback<VersionInfo>() {


            @Override
            public void onS(Response<VersionInfo> response, VersionInfo versionInfo) {
                AppUpdateUtils.compareVersion(versionInfo.data,false);
            }
        });

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        binding.bar.tvTitle.setText("工作台");

        binding.flCard.setOnClickListener(v -> {
            ActivityUtils.startActivity(UserInfoActivity.class);
        });

        binding.rvTab.setLayoutManager(new GridLayoutManager(this, 4));
        mAdapter = new MainTabAdapter();
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            MainTab item = mAdapter.getItem(position);
            if (item.listener != null) {
                item.listener.onClick(view);
            } else {
                ActivityUtils.startActivity(item.clazz);
            }
        });
        binding.rvTab.setAdapter(mAdapter);

        initUI();
        binding.tvUser.setText(UserManager.getDeptName() + " — " + UserManager.getUser().nickName);
        binding.tvUsername.setText("NAME：" + UserManager.getUser().userName);
        ImgUtils.load(activity, UserManager.getUser().avatar, binding.ivAvater);
        mAdapter.setNewInstance(ImgManager.getMenu());
        binding.srf.setOnRefreshListener(this);
        getVersionData();

    }


    private void initUI() {
        binding.llCard.setVisibility(View.VISIBLE);
        Role role = UserManager.getUser().getRole();
        switch (role) {
            case TEACHER:
                binding.llCard.setBackgroundResource(R.drawable.ic_main_bg);
                binding.llTodayCourse.setVisibility(View.VISIBLE);
                binding.llAllCourse.setVisibility(View.VISIBLE);
                binding.llTodayTask.setVisibility(View.GONE);
                binding.llAllTask.setVisibility(View.GONE);
                binding.llTop.setVisibility(View.VISIBLE);
                getTeacherData();
                break;
            case BASE:
                binding.llCard.setBackgroundResource(R.drawable.ic_main_bg2);
                binding.llTop.setVisibility(View.GONE);
                break;
            case ORGANIZER:
            case UNDERTAKER:
                binding.llCard.setBackgroundResource(R.drawable.ic_main_bg2);
                binding.llTop.setVisibility(View.GONE);
                break;
            case EDUCATION:
                binding.llCard.setBackgroundResource(R.drawable.ic_main_bg);
                binding.llTop.setVisibility(View.VISIBLE);
                getMsgData();
                break;
            case TRAIN:
                binding.llCard.setBackgroundResource(R.drawable.ic_main_bg);
                binding.llTop.setVisibility(View.VISIBLE);
                getMsgData();
                break;
            case LOGISTICS:
                binding.llCard.setBackgroundResource(R.drawable.ic_main_bg);
                binding.llTop.setVisibility(View.VISIBLE);
                getMsgData();
                break;
            default:
                binding.llCard.setBackgroundResource(R.drawable.ic_main_bg2);
                binding.llTop.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onRefresh() {
        getData();
    }

    @Override
    protected void onStart() {
        super.onStart();
        getData();
    }

    private void getData() {
        App.api.getInfo().enqueue(new JCallback<LoginUser>() {
            @Override
            public void onS(Response<LoginUser> response, LoginUser loginUser) {
                UserManager.initUser(loginUser);
                initUI();
                binding.tvUser.setText(loginUser.user.dept.deptName + " — " + loginUser.user.nickName);
                binding.tvUsername.setText("NAME：" + loginUser.user.userName);
                ImgUtils.load(activity, loginUser.user.avatar, binding.ivAvater);
                mAdapter.setNewInstance(ImgManager.getMenu());
            }

            @Override
            public void onFinish() {
                super.onFinish();
                binding.srf.setRefreshing(false);
            }
        });
    }

    private void getMsgData() {
        App.api.queryMessageCount().enqueue(new JCallback<MsgCount>() {
            @Override
            public void onS(Response<MsgCount> response, MsgCount msgCount) {
                binding.tvTodayTaskCnt.setText(msgCount.data.addTask + "");
                binding.tvAllTaskCnt.setText(msgCount.data.allTask + "");
                binding.tvNoticeCnt.setText(msgCount.data.message + "");
            }
        });
    }

    private void getTeacherData() {
        App.api.queryTeacherInfoCount(UserManager.getUid()).enqueue(new JCallback<MsgCount>() {
            @Override
            public void onS(Response<MsgCount> response, MsgCount msgCount) {
                binding.tvTodayCourseCnt.setText(msgCount.data.dateNowCourseCount + "");
                binding.tvAllCourseCnt.setText(msgCount.data.allCourseCount + "");
                binding.tvNoticeCnt.setText(msgCount.data.dateNowNoticeCount + "");
            }


        });
    }
}