package com.tencent.qcloud.tuikit.tuichat.ui.page;

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

import androidx.annotation.Nullable;

import com.tencent.qcloud.tuicore.TUIConstants;
import com.tencent.qcloud.tuicore.util.ToastUtil;
import com.tencent.qcloud.tuikit.tuichat.R;
import com.tencent.qcloud.tuikit.tuichat.TUIChatConstants;
import com.tencent.qcloud.tuikit.tuichat.bean.ChatInfo;
import com.tencent.qcloud.tuikit.tuichat.presenter.C2CChatPresenter;
import com.tencent.qcloud.tuikit.tuichat.util.TUIChatLog;
import com.tencent.qcloud.tuikit.tuichat.util.TUIChatUtils;
import com.zxhl.cms.utils.ActivityManager;

public class TUIC2CChatActivity extends TUIBaseChatActivity {
    private static final String TAG = TUIC2CChatActivity.class.getSimpleName();

    private TUIC2CChatFragment chatFragment;
    private C2CChatPresenter presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    }

    @Override
    public void initChat(ChatInfo chatInfo) {
        ActivityManager.getInstance().addActivity(this);
        TUIChatLog.i(TAG, "inti chat " + chatInfo);
        if (!TUIChatUtils.isC2CChat(chatInfo.getType())) {
            TUIChatLog.e(TAG, "init C2C chat failed , chatInfo = " + chatInfo);
            ToastUtil.toastShortMessage("init c2c chat failed.");
        }
        chatFragment = new TUIC2CChatFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(TUIChatConstants.CHAT_INFO, chatInfo);
        bundle.putString("conversationHeadIconUrl", getIntent().getStringExtra("conversationHeadIconUrl"));
        if (!TextUtils.isEmpty(getIntent().getStringExtra(TUIConstants.TUIChat.MESSAGE_CONTENT))) {
            bundle.putString(TUIConstants.TUIChat.MESSAGE_CONTENT, getIntent().getStringExtra(TUIConstants.TUIChat.MESSAGE_CONTENT));
        }
        chatFragment.setArguments(bundle);
        presenter = new C2CChatPresenter();
        presenter.initListener();
        chatFragment.setPresenter(presenter);
        getSupportFragmentManager().beginTransaction().replace(R.id.empty_view, chatFragment).commitAllowingStateLoss();
    }

    @Override
    public void onBackPressed() {
        if (chatFragment != null && chatFragment.isShowInputView()) {
            chatFragment.hideInputMoreView();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.fade_out, R.anim.fade_in);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.getInstance().finishActivity(this);
    }
}
