package com.kdanmobile.kdan_others_library_for_android.dash2fullscreen;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.kdanmobile.kdan_others_library_for_android.R;

public abstract class BaseDash2FullScreenAdActivity extends AppCompatActivity {
    private Dash2FullScreenAdController controller;
    private boolean hasOpenLink = false;
    private boolean isFullScreen = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setFullScreen();
        setContentView(R.layout.dailog_dash2_full_screen_ad);
        findViewById(R.id.imageView_dash2_full_screen_ad).setOnClickListener(this::onClickImage);
        findViewById(R.id.button_dash2_full_screen_ad_view_more).setOnClickListener(this::onClickViewMoreBtn);

        TextView subtitle = (TextView) findViewById(R.id.textView_dash2_full_screen_ad_t2);
        subtitle.setText(Html.fromHtml(getString(R.string.dash2_full_screen_ad_subtitle)));

        controller = new Dash2FullScreenAdController(this);
    }

    private void setFullScreen() {
        if (isFullScreen) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!hasOpenLink) {
            controller.increaseLeaveDirectlyCount();
            onLeaveDirectlyHook();
        }
    }

    private void onClickImage(View v) {
        hasOpenLink = true;
        openWebsite();
        onClickImageHook();
    }

    private void onClickViewMoreBtn(View v) {
        hasOpenLink = true;
        openWebsite();
        onClickViewMoreBtnHook();
    }

    protected final void setIsFullScreen(boolean b) {
        this.isFullScreen = b;
    }

    protected void openWebsite() {
        String url = "https://creativestore.kdanmobile.com/subscription/creativity365FREEDASH2?utm_source=App&utm_campaign=App_Android_PDF_C365&utm_medium=Android_PDF";
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    protected abstract void onLeaveDirectlyHook();

    protected abstract void onClickImageHook();

    protected abstract void onClickViewMoreBtnHook();
}
