package com.gh.gamecenter.newsdetail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.therouter.router.Route;
import com.gh.base.DownloadToolbarActivity;
import com.gh.gamecenter.WebActivity;
import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.common.constant.RouteConsts;
import com.gh.gamecenter.common.utils.ExtensionsKt;
import com.gh.gamecenter.core.utils.MtaHelper;
import com.gh.gamecenter.feature.entity.NewsEntity;

import kotlin.Pair;


/**
 * 文章详情页面 要启动该页面 需要传入一下参数 放入 EssayEntity中传过来 文章 id 文章标题 title 文章发表时间 time
 *
 * @author 黄壮华
 */
@Route(path = RouteConsts.activity.newsDetailActivity)
public class NewsDetailActivity extends DownloadToolbarActivity {

    /**
     * 启动新闻详情页面
     */
    public static void startNewsDetailActivity(Context context, NewsEntity newsEntity, String entrance) {
        MtaHelper.onEvent("详情页面", "文章详情", newsEntity != null ? newsEntity.getTitle() : "");
        if (newsEntity != null && !TextUtils.isEmpty(newsEntity.getLink())) {
            context.startActivity(WebActivity.getIntentByNews(context, newsEntity, entrance));
        } else {
            Bundle bundle = new Bundle();
            bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance);
            bundle.putParcelable(NewsEntity.TAG, newsEntity);
            Intent intent = getTargetIntent(context, NewsDetailActivity.class, NewsDetailFragment.class, bundle);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }

    /**
     * 在收藏启动新闻详情页面
     */
    public static void startNewsDetailByCollection(Context context, NewsEntity newsEntity, String entrance) {
        MtaHelper.onEvent("详情页面", "文章详情", newsEntity != null ? newsEntity.getTitle() : "");
        if (newsEntity != null && !TextUtils.isEmpty(newsEntity.getLink())) {
            Intent intent = WebActivity.getIntentByNews(context, newsEntity, entrance);
            context.startActivity(intent);
        } else {
            Bundle bundle = new Bundle();
            bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance);
            bundle.putParcelable(NewsEntity.TAG, newsEntity);
            bundle.putBoolean(NewsDetailFragment.KEY_COLLECTIONNEWS, true);
            Intent intent = getTargetIntent(context, NewsDetailActivity.class, NewsDetailFragment.class, bundle);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }

    }

    @NonNull
    public static Intent getIntentById(Context context, String newsId, boolean isCollectionNews, String entrance) {
        Bundle bundle = new Bundle();
        bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance);
        bundle.putBoolean(NewsDetailFragment.KEY_COLLECTIONNEWS, isCollectionNews);
        bundle.putString(EntranceConsts.KEY_NEWSID, newsId);
        return getTargetIntent(context, NewsDetailActivity.class, NewsDetailFragment.class, bundle);
    }

    @NonNull
    public static Intent getIntentById(Context context, String newsId, String entrance) {
        Bundle bundle = new Bundle();
        bundle.putString(EntranceConsts.KEY_ENTRANCE, entrance);
        bundle.putString(EntranceConsts.KEY_NEWSID, newsId);
        return getTargetIntent(context, NewsDetailActivity.class, NewsDetailFragment.class, bundle);
    }

    @Override
    protected Intent provideNormalIntent() {
        return getTargetIntent(this, NewsDetailActivity.class, NewsDetailFragment.class, getIntent().getExtras());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExtensionsKt.updateStatusBarColor(this, com.gh.gamecenter.common.R.color.ui_surface, com.gh.gamecenter.common.R.color.ui_surface);
    }

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

    @Override
    protected void onDarkModeChanged() {
        super.onDarkModeChanged();
        ExtensionsKt.updateStatusBarColor(this, com.gh.gamecenter.common.R.color.ui_surface, com.gh.gamecenter.common.R.color.ui_surface);
    }

    @Override
    public Pair<String, String> getBusinessId() {
        NewsEntity newsEntity = null;
        Bundle bundle = getIntent().getBundleExtra(NORMAL_FRAGMENT_BUNDLE);
        if (bundle != null) {
            newsEntity = bundle.getParcelable(NewsEntity.TAG);
        }
        if (getIntent().getStringExtra(EntranceConsts.KEY_NEWSID) != null) {
            return new Pair(getIntent().getStringExtra(EntranceConsts.KEY_NEWSID), "");
        } else if (newsEntity != null) {
            return new Pair(newsEntity.getId(), "");
        } else {
            return super.getBusinessId();
        }
    }
}
