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.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.epson.mobilephone.common.license.SimpleMessageDialogFragment;

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

public class LicenseTopActivity extends AppCompatActivity implements SimpleMessageDialogFragment.DialogCallback {
    private static final String DIALOG_ID = "dialog";
    private static final int DIALOG_ID_DISAGREE = 2;
    private static final int DIALOG_ID_NEW_LICENSE = 1;
    private static final String KEY_LICENSE_INFO = "license_info";
    private static final String KEY_USER_SURVEY_INFO = "user_survey_info";
    private static final int REQUEST_CODE_USER_SURVEY_INVITATION = 2;
    private LicenseInfo mLicenseInfo;
    private UserSurveyInfo mUserSurveyInfo;

    public void onBackPressed() {
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_license_top);
        ((ViewGroup) findViewById(R.id.licenseViewGroup)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startDocumentDisplayActivity(1);
            }
        });
        ((ViewGroup) findViewById(R.id.privacyStatementViewGroup)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startDocumentDisplayActivity(2);
            }
        });
        ((Button) findViewById(R.id.agreeButton)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                onAgree();
            }
        });
        ((Button) findViewById(R.id.disagreeButton)).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                onDisagree();
            }
        });
        if (!initField(getIntent())) {
            setResult(0);
            finish();
            return;
        }
        ((TextView) findViewById(R.id.appNameText)).setText(mLicenseInfo.getApplicationName(this));
        if (bundle == null) {
            showLicenseChangeDialogIfNeeded(this, mLicenseInfo, mUserSurveyInfo);
        }
    }

    private boolean initField(Intent intent) {
        if (intent == null) {
            return false;
        }
        mLicenseInfo = (LicenseInfo) intent.getSerializableExtra(KEY_LICENSE_INFO);
        if (mLicenseInfo == null) {
            return false;
        }
        mUserSurveyInfo = (UserSurveyInfo) intent.getSerializableExtra(KEY_USER_SURVEY_INFO);
        return true;
    }

    private void showLicenseChangeDialogIfNeeded(@NonNull Context context, @NonNull LicenseInfo licenseInfo, UserSurveyInfo userSurveyInfo) {
        String str;
        switch (licenseInfo.getLicenseAgreement(context)) {
            case 0:
                str = getString(R.string.EULA_Title) + Constants.BREAK_LINE + getString(R.string.Privacy_Statement_Title);
                break;
            case 1:
                str = getString(R.string.EULA_Title);
                break;
            case 2:
                str = getString(R.string.Privacy_Statement_Title);
                break;
            case 3:
                setResult(-1);
                if (userSurveyInfo == null || userSurveyInfo.getResponseStatus(context) >= 2) {
                    finish();
                    return;
                } else {
                    startUserSurveyInvitationActivity();
                    return;
                }
            default:
                return;
        }
        SimpleMessageDialogFragment.newInstance(getString(R.string.Update_Message) + Constants.BREAK_LINE + str, 1).show(getSupportFragmentManager(), DIALOG_ID);
    }

    private void onAgree() {
        LicenseInfo licenseInfo = mLicenseInfo;
        if (licenseInfo == null) {
            finish();
            return;
        }
        licenseInfo.setLicenceAgreement(this);
        setResult(-1);
        startUserSurveyInvitationActivity();
    }

    private void startUserSurveyInvitationActivity() {
        if (mLicenseInfo == null) {
            finish();
            return;
        }
        UserSurveyInfo userSurveyInfo = mUserSurveyInfo;
        if (userSurveyInfo == null) {
            finish();
        } else if (userSurveyInfo.getResponseStatus(this) >= 2) {
            finish();
        } else {
            startActivityForResult(UserSurveyInvitationActivity.getStartWithDialogIntent(this, mUserSurveyInfo, mLicenseInfo), 2);
        }
    }

    private void onDisagree() {
        SimpleMessageDialogFragment.newInstance(getString(R.string.Disagree_License_Button_Message), 2).show(getSupportFragmentManager(), DIALOG_ID);
    }

    private void startDocumentDisplayActivity(int i) {
        LicenseInfo licenseInfo = mLicenseInfo;
        if (licenseInfo != null) {
            startActivity(InfoDisplayActivity.getStartIntent(this, licenseInfo, i));
        }
    }

    public void onButtonClicked(int i) {
        switch (i) {
            case 1:
                return;
            case 2:
                setResult(0);
                finish();
                return;
            default:
                return;
        }
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 2) {
            setResult(-1);
            finish();
        }
    }

    public static Intent getStartIntent(Context context, @NonNull LicenseInfo licenseInfo, @Nullable UserSurveyInfo userSurveyInfo) {
        Intent intent = new Intent(context, LicenseTopActivity.class);
        intent.putExtra(KEY_LICENSE_INFO, licenseInfo);
        intent.putExtra(KEY_USER_SURVEY_INFO, userSurveyInfo);
        return intent;
    }
}
