package com.secspace.lib.common.function;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.secspace.lib.common.R;
import com.secspace.lib.common.utils.IntentUtil;
import com.secspace.lib.common.utils.Prefs;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class BuyCodeDialog implements DialogInterface.OnDismissListener, View.OnClickListener {
    private static final String TAG = "BuyCodeDialog";
    private Activity mActivity;
    private Context mContext;
    private android.app.AlertDialog mDialog;
    private Button btnAccept;
    private View layout;

    public BuyCodeDialog(Activity activity) {
        this.mActivity = activity;
        mContext = activity.getApplicationContext();
    }

    public BuyCodeDialog show() {
        if (mDialog == null) {
            initView();
            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mActivity);
            mDialog = builder.create();
            mDialog.setCancelable(false);
            Window window = mDialog.getWindow();
            window.setBackgroundDrawableResource(R.drawable.dialog_corner_bg);

            mDialog.setView(layout);
            mDialog.show();
        }
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        return this;
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(mActivity);
        layout = inflater.inflate(R.layout.buycode_layout, null);
        btnAccept = layout.findViewById(R.id.btn_accept);
        btnAccept.setOnClickListener(this);
    }

    public void dismiss() {
        if (mDialog != null && mDialog.isShowing())
            mDialog.dismiss();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        mActivity = null;
        mDialog = null;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_accept) {
            ActivationHelper.INSTANCE.active(mActivity, new ActivationHelper.ActivationListener() {

                @Override
                public void activated() {
                    IntentUtil.startEduLauncher(mContext);
                }
            }, true);
            SwitchOutMdm.INSTANCE.exit(mContext);
            dismiss();
        }
    }
}
