package com.ccwangluo.im;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.ccwangluo.ccim.CCIMClient;
import com.ccwangluo.ccim.listener.OnPushMsgListener;
import com.ccwangluo.ccim.listener.ResultEventCallback;
import com.ccwangluo.ccim.modle.Contact;
import com.ccwangluo.ccim.modle.MessageContent;
import com.ccwangluo.ccim.modle.NetStatus;
import com.ccwangluo.ccim.modle.User;
import com.ccwangluo.im.data.LoginViewModel;
import com.ccwangluo.im.databinding.ActivityMain2Binding;
import com.ccwangluo.im.ui.login.LoginActivity;
import com.ccwangluo.im.util.ToastUtil;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import org.greenrobot.eventbus.EventBus;

public class MainActivity2 extends AppCompatActivity {

    private LoginViewModel loginViewModel;
    private ActivityMain2Binding binding;
    private NavController navController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMain2Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main2);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//添加默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true);

        loginViewModel = new ViewModelProvider(this)
                .get(LoginViewModel.class);
        loginViewModel.getContactList();

        CCIMClient.getInstance().registerMessageListener(new OnPushMsgListener() {

            @Override
            public void pushPrivateMsg(MessageContent msg) {
                loginViewModel.addPrivateMsg(msg);
                loginViewModel.getContactList();
                EventBus.getDefault().post(msg);
            }

            @Override
            public void pushChannelMsg(MessageContent msg) {
                loginViewModel.addChannelMsg(msg);
                EventBus.getDefault().post(msg);
            }

            @Override
            public void pushNewContact(Contact contact) {
                loginViewModel.getContactList();
            }

            @Override
            public void pushDeleteContact(Long contactId) {
                loginViewModel.getContactList();
            }

            @Override
            public void pushContactNetStatus(Long contactId, NetStatus status) {
                loginViewModel.getContactList();
            }

            @Override
            public void pushContactNewReq(User user) {
                String[] strings = {user.getUsername()};
                AlertDialog alertDialog = new AlertDialog
                        .Builder(MainActivity2.this)
                        .setTitle("contact request list  点击验证好友")
                        .setItems(strings, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                CCIMClient.getInstance().checkContactReq(user.getUserId(), true, new ResultEventCallback<Void>() {
                                    @Override
                                    public void onSuccess(Void unused) {
                                        Toast.makeText(MainActivity2.this, "验证好友发送成功", Toast.LENGTH_SHORT).show();
                                        loginViewModel.getContactList();
                                    }

                                    @Override
                                    public void onFailed(int errorCode) {
                                        Toast.makeText(MainActivity2.this, "验证好友失败 : " + errorCode, Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }).setNegativeButton("关闭", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                CCIMClient.getInstance().checkContactReq(user.getUserId(), false, new ResultEventCallback<Void>() {
                                    @Override
                                    public void onSuccess(Void unused) {

                                    }

                                    @Override
                                    public void onFailed(int errorCode) {

                                    }
                                });
                            }
                        })
                        .create();
                alertDialog.show();

            }

            @Override
            public void pushUserMutedRes(Long mutedExpiredAt) {
                ToastUtil.show(getApplicationContext(), "被禁言到" + mutedExpiredAt);
            }
        });
        startActivity(new Intent(this,LoginActivity.class));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:// 点击返回图标事件
                navController.navigateUp();
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CCIMClient.getInstance().logout();
    }
}