package com.epson.mobilephone.common.license;

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


import android.support.p003v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.epson.mobilephone.common.license.SimpleMessageDialogFragment;
import epson.common.Constants;

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) {
        if (i == 1) {
        }
    }

    protected void onCreate(Bundle bundle) {
        UserSurveyInfo userSurveyInfo;
        super.onCreate(bundle);
        setContentView(C1066R.layout.activity_user_survey_invitation);
        setSupportActionBar((Toolbar) findViewById(C1066R.C1068id.toolbar));
        setTitle(C1066R.string.GALicense);
        Button button = (Button) findViewById(C1066R.C1068id.disagreeButton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                UserSurveyInvitationActivity.this.finishWithStatus(false);
            }
        });
        Button button2 = (Button) findViewById(C1066R.C1068id.agreeButton);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                UserSurveyInvitationActivity.this.finishWithStatus(true);
            }
        });
        ((ViewGroup) findViewById(C1066R.C1068id.privacyStatementViewGroup)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                UserSurveyInvitationActivity.this.startPrivacyStatementDisplayActivity();
            }
        });
        TextView textView = (TextView) findViewById(C1066R.C1068id.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 = C1073Util.getStringFromRawResource(getApplicationContext(), C1066R.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(C1066R.C1068id.appNameText)).setText(applicationName);
        }
    }

    private void showUpdateDialog() {
        SimpleMessageDialogFragment.newInstance(getString(C1066R.string.Update_Message) + Constants.BREAK_LINE + getString(C1066R.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);
    }
}
