package com.epson.mobilephone.common.license;

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


import android.support.annotation.VisibleForTesting;
import android.support.p003v7.app.ActionBar;

import android.support.p003v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.TextView;
import java.io.Serializable;

public class InfoDisplayActivity extends AppCompatActivity {
    private static final String KEY_SCROLL_Y = "scroll_y";
    private static final String PARAM_KEY_DOCUMENT_TYPE = "document_type";
    private static final String PARAM_KEY_LICENSE_INFO = "license_info";
    private static final String PARAM_KEY_TITLE_AND_DOCUMENT = "class_title_and_document";
    private RestoreScrollView mScrollView;

    public interface TitleAndDocument extends Serializable {
        @NonNull
        String getDocumentString(@NonNull Context context);

        @NonNull
        String getTitle(@NonNull Context context);
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        supportRequestWindowFeature(8);
        setContentView(C1066R.layout.activity_info_display);
        setSupportActionBar((Toolbar) findViewById(C1066R.C1068id.toolbar));
        TitleAndDocument titleAndDocumentFromIntent = getTitleAndDocumentFromIntent(getIntent());
        if (titleAndDocumentFromIntent == null) {
            finish();
            return;
        }
        ((TextView) findViewById(C1066R.C1068id.contentText)).setText(titleAndDocumentFromIntent.getDocumentString(getApplicationContext()));
        mScrollView = (RestoreScrollView) findViewById(C1066R.C1068id.mainScrollView);
        setTitle(titleAndDocumentFromIntent.getTitle(getApplicationContext()));
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        if (bundle != null) {
            mScrollView.setScrollY(bundle.getInt(KEY_SCROLL_Y, 0));
        }
    }

    @Nullable
    @VisibleForTesting
    static TitleAndDocument getTitleAndDocumentFromIntent(@Nullable Intent intent) {
        if (intent == null) {
            return null;
        }
        TitleAndDocument internalTitleAndDocumentFromIntent = getInternalTitleAndDocumentFromIntent(intent);
        if (internalTitleAndDocumentFromIntent != null) {
            return internalTitleAndDocumentFromIntent;
        }
        return getExternalTitleAndDocumentFromIntent(intent);
    }

    @Nullable
    private static TitleAndDocument getInternalTitleAndDocumentFromIntent(@NonNull Intent intent) {
        LicenseInfo licenseInfo = (LicenseInfo) intent.getSerializableExtra(PARAM_KEY_LICENSE_INFO);
        if (licenseInfo == null) {
            return null;
        }
        return new TitleAndTextInternal(intent.getIntExtra(PARAM_KEY_DOCUMENT_TYPE, 1), licenseInfo);
    }

    @Nullable
    private static TitleAndDocument getExternalTitleAndDocumentFromIntent(@NonNull Intent intent) {
        return (TitleAndDocument) intent.getSerializableExtra(PARAM_KEY_TITLE_AND_DOCUMENT);
    }

    private static int getTitleStringId(int i) {
        switch (i) {
            case 2:
                return C1066R.string.Privacy_Statement_Title;
            case 3:
                return C1066R.string.OSS_License_Title;
            default:
                return C1066R.string.EULA_Title;
        }
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != 16908332) {
            return super.onOptionsItemSelected(menuItem);
        }
        finish();
        return true;
    }

    protected void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putInt(KEY_SCROLL_Y, mScrollView.getLastY());
    }

    @NonNull
    public static Intent getStartIntent(@NonNull Context context, LicenseInfo licenseInfo, int i) {
        Intent intent = new Intent(context, InfoDisplayActivity.class);
        intent.putExtra(PARAM_KEY_LICENSE_INFO, licenseInfo);
        intent.putExtra(PARAM_KEY_DOCUMENT_TYPE, i);
        return intent;
    }

    @NonNull
    public static Intent getStartIntent(@NonNull Context context, @NonNull TitleAndDocument titleAndDocument) {
        Intent intent = new Intent(context, InfoDisplayActivity.class);
        intent.putExtra(PARAM_KEY_TITLE_AND_DOCUMENT, titleAndDocument);
        return intent;
    }

    @VisibleForTesting
    static class TitleAndTextInternal implements TitleAndDocument {
        private final int mDocumentType;
        private final LicenseInfo mLicenseInfo;

        TitleAndTextInternal(int i, @NonNull LicenseInfo licenseInfo) {
            mDocumentType = i;
            mLicenseInfo = licenseInfo;
        }

        @NonNull
        public String getTitle(@NonNull Context context) {
            return context.getString(InfoDisplayActivity.getTitleStringId(mDocumentType));
        }

        @NonNull
        public String getDocumentString(@NonNull Context context) {
            return mLicenseInfo.getDocumentString(context, mDocumentType);
        }
    }
}
