package epson.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

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

public class ScanSettingsAdvanceDensityActivity extends ActivityIACommon {
    private static final int DEFAULT_DENSITY = 128;
    private static final String PARAM_KEY_DENSITY = "SCAN_SETTINGS_LAST_DENSITY";
    private static final String PARAM_KEY_DENSITY_STATUS = "SCAN_SETTINGS_LAST_DENSITY_STATUS";
    private boolean densityStatus;
    private int densityValue;
    private SeekBar.OnSeekBarChangeListener mSeekDensityChangeListener = new SeekBar.OnSeekBarChangeListener() {
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        public void onProgressChanged(SeekBar seekBar, int i, boolean z) {
            int unused = ScanSettingsAdvanceDensityActivity.this.densityValue = 255 - i;
            ScanSettingsAdvanceDensityActivity.this.tvDensityValue.setText(String.format(Constants.DENSITY_VALUE, new Object[]{Integer.valueOf(255 - ScanSettingsAdvanceDensityActivity.this.densityValue)}));
        }
    };
    private View.OnClickListener mToogleDensityListener = new View.OnClickListener() {
        public void onClick(View view) {
            ScanSettingsAdvanceDensityActivity scanSettingsAdvanceDensityActivity = ScanSettingsAdvanceDensityActivity.this;
            boolean unused = scanSettingsAdvanceDensityActivity.densityStatus = scanSettingsAdvanceDensityActivity.toggleDensity.isChecked();
            if (!ScanSettingsAdvanceDensityActivity.this.densityStatus) {
                ScanSettingsAdvanceDensityActivity.this.seekDensity.setProgress(127);
                ScanSettingsAdvanceDensityActivity.this.seekDensity.setEnabled(false);
                return;
            }
            ScanSettingsAdvanceDensityActivity.this.seekDensity.setEnabled(true);
        }
    };
    private SeekBar seekDensity;
    private Switch toggleDensity;
    private TextView tvDensityValue;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.scan_settings_advance_density);
        setActionBar(R.string.brightness, true);
        Bundle extras = getIntent().getExtras();
        extras.getInt(Constants.SCAN_SETTINGS_DETAIL_TITLE);
        densityValue = extras.getInt(PARAM_KEY_DENSITY);
        densityStatus = extras.getBoolean(PARAM_KEY_DENSITY_STATUS);
        seekDensity = (SeekBar) findViewById(R.id.seekDensity);
        seekDensity.setProgress(this.densityValue);
        seekDensity.setOnSeekBarChangeListener(mSeekDensityChangeListener);
        tvDensityValue = (TextView) findViewById(R.id.tvDensityValue);
        tvDensityValue.setText(String.format(Constants.DENSITY_VALUE, new Object[]{String.valueOf(255 - densityValue)}));
        seekDensity.setProgress(255 - densityValue);
        toggleDensity = (Switch) findViewById(R.id.toggleDensity);
        toggleDensity.setChecked(this.densityStatus);
        toggleDensity.setOnClickListener(mToogleDensityListener);
        if (!this.toggleDensity.isChecked()) {
            seekDensity.setEnabled(false);
        } else {
            seekDensity.setEnabled(true);
        }
    }

    public void onBackPressed() {
        Intent intent = getIntent();
        intent.putExtra(PARAM_KEY_DENSITY, densityValue);
        intent.putExtra(PARAM_KEY_DENSITY_STATUS, densityStatus);
        setResult(-1, intent);
        finish();
        super.onBackPressed();
    }

    @NonNull
    public static Intent getStartIntent(@NonNull Context context, int i, boolean z) {
        Intent intent = new Intent(context, ScanSettingsAdvanceDensityActivity.class);
        intent.putExtra(PARAM_KEY_DENSITY, i);
        intent.putExtra(PARAM_KEY_DENSITY_STATUS, z);
        return intent;
    }

    public static int getDensityFromReturnIntent(@Nullable Intent intent) {
        if (intent == null) {
            return 128;
        }
        return intent.getIntExtra(PARAM_KEY_DENSITY, 128);
    }

    public static boolean getChangeAble(@Nullable Intent intent) {
        if (intent == null) {
            return false;
        }
        return intent.getBooleanExtra(PARAM_KEY_DENSITY_STATUS, false);
    }
}
