package com.epson.mobilephone.common.license;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import epson.common.Constants;
import epson.print.R;

/**
 *
 */
public class UserSurveyInvitationActivity extends AppCompatActivity implements SimpleMessageDialogFragment.DialogCallback {
    private static final int DIALOG_ID_CHANGE_DIALOG = 1;
    private static final String PARAM_DISPLAY_CHANGE_DIALOG = "display_change_dialog";
    private static final String PARAM_LICENSE_INFO = "license_info";
    private static final String PARAM_USER_SURVEY_INFO = "user_survey_info";
    private boolean mDisplayChangeDialog;
    private LicenseInfo mLicenseInfo;
    private UserSurveyInfo mUserSurveyInfo;

    public void onButtonClicked(int i) {

    }

    protected void onCreate(Bundle bundle) {
        UserSurveyInfo userSurveyInfo;
        super.onCreate(bundle);
        setContentView(R.layout.activity_user_survey_invitation);

        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        setTitle(R.string.GALicense);
        Button button = findViewById(R.id.disagreeButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                finishWithStatus(false);
            }
        });
        Button button2 = (Button) findViewById(R.id.agreeButton);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                finishWithStatus(true);
            }
        });
        ((ViewGroup) findViewById(R.id.privacyStatementViewGroup)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startPrivacyStatementDisplayActivity();
            }
        });
        TextView textView = (TextView) findViewById(R.id.analytics_content);
        mDisplayChangeDialog = false;
        Intent intent = getIntent();
        if (intent != null) {
            mUserSurveyInfo = (UserSurveyInfo) intent.getSerializableExtra(PARAM_USER_SURVEY_INFO);
            mLicenseInfo = (LicenseInfo) intent.getSerializableExtra(PARAM_LICENSE_INFO);
            String str = null;
            UserSurveyInfo userSurveyInfo2 = mUserSurveyInfo;
            if (userSurveyInfo2 != null) {
                str = userSurveyInfo2.getUserSurveyInvitationText(this);
            }
            if (str == null) {
                str = Util.getStringFromRawResource(getApplicationContext(), R.raw.user_survey);
            }
            textView.setText(str);
            changeButtonText(button2, 1, mUserSurveyInfo, this);
            changeButtonText(button, 0, mUserSurveyInfo, this);
            updateAppNameText(mLicenseInfo);
            mDisplayChangeDialog = intent.getBooleanExtra(PARAM_DISPLAY_CHANGE_DIALOG, false);
            if (mDisplayChangeDialog && bundle == null && (userSurveyInfo = mUserSurveyInfo) != null && userSurveyInfo.getResponseStatus(this) == 1) {
                showUpdateDialog();
            }
        }
    }

    /**
     * 跳转到用户隐私协议页面
     */
    private void startPrivacyStatementDisplayActivity() {
        LicenseInfo licenseInfo = mLicenseInfo;
        if (licenseInfo != null) {
            startActivity(InfoDisplayActivity.getStartIntent(this, licenseInfo, 2));
        }
    }

    private void updateAppNameText(LicenseInfo licenseInfo) {
        String applicationName;
        if (licenseInfo != null && (applicationName = licenseInfo.getApplicationName(this)) != null) {
            ((TextView) findViewById(R.id.appNameText)).setText(applicationName);
        }
    }

    private void showUpdateDialog() {
        SimpleMessageDialogFragment.newInstance(getString(R.string.Update_Message) + Constants.BREAK_LINE + getString(R.string.GALicense), 1).show(getSupportFragmentManager(), (String) null);
    }

    private void changeButtonText(Button button, int i, UserSurveyInfo userSurveyInfo, @NonNull Context context) {
        String buttonString;
        if (userSurveyInfo != null && (buttonString = userSurveyInfo.getButtonString(context, i)) != null) {
            button.setText(buttonString);
        }
    }

    private void finishWithStatus(boolean z) {
        UserSurveyInfo userSurveyInfo = mUserSurveyInfo;
        if (userSurveyInfo != null) {
            userSurveyInfo.setUserSurveyAgreement(this, z);
        }
        setResult(z ? -1 : 0);
        finish();
    }

    public void onBackPressed() {
        if (!mDisplayChangeDialog) {
            finishWithStatus(false);
        }
    }

    @NonNull
    private static Intent getStartIntent(@NonNull Context context, UserSurveyInfo userSurveyInfo, LicenseInfo licenseInfo, boolean z) {
        Intent intent = new Intent(context, UserSurveyInvitationActivity.class);
        intent.putExtra(PARAM_USER_SURVEY_INFO, userSurveyInfo);
        intent.putExtra(PARAM_DISPLAY_CHANGE_DIALOG, z);
        intent.putExtra(PARAM_LICENSE_INFO, licenseInfo);
        return intent;
    }

    @NonNull
    static Intent getStartWithDialogIntent(@NonNull Context context, UserSurveyInfo userSurveyInfo, LicenseInfo licenseInfo) {
        return getStartIntent(context, userSurveyInfo, licenseInfo, true);
    }

    @NonNull
    public static Intent getStartIntent(@NonNull Context context, @NonNull UserSurveyInfo userSurveyInfo, LicenseInfo licenseInfo) {
        return getStartIntent(context, userSurveyInfo, licenseInfo, false);
    }
}
