package com.xplora.xpsettings.Activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Data.DataManager;
import com.xplora.xpsettings.Utils.Constant;
import com.xplora.xpsettings.Utils.ResUtils;


public class ChangeProgressActivity extends BaseActivity implements SeekBar.OnSeekBarChangeListener {
    int mViewType;
    int initValue;
    int lastValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_progress);
        initDatas();
        initView();
    }

    private void initDatas() {
        mViewType = getIntent().getIntExtra(Constant.INENT_VIEW_TYPE, 0);
        if (mViewType == 3) {
            initValue = DataManager.getBrightness();
        } else {
            initValue = DataManager.getVolume(mViewType);
        }
    }

    @SuppressLint("DefaultLocale")
    private void initView() {
        ImageView imageView = findViewById(R.id.icon);
        TextView titleText = findViewById(R.id.title);
        SeekBar seekBar = findViewById(R.id.seekbar);

        if (imageView != null) {
            int imageId = ResUtils.getImageId("ic_seekbar_", mViewType);
            imageView.setImageResource(imageId);
        }

        if (titleText != null) {
            if (mViewType == 3) {
                String[] stringList = getResources().getStringArray(R.array.display_title_array);
                titleText.setText(stringList[0]);
            } else {
                String[] stringList = getResources().getStringArray(R.array.volume_title_array);
                String[] soundModeList = getResources().getStringArray(R.array.soundmode_title_array);
                String title = stringList[mViewType] + " " + soundModeList[4];
                titleText.setText(title);
            }
        }
        if (seekBar != null) {
            seekBar.setProgress(initValue);
            seekBar.setOnSeekBarChangeListener(this);
        }
    }

    /*
     * SeekBar滚动时的回调函数
     */
    @SuppressLint("SetTextI18n")
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (seekBar.getId() == R.id.seekbar) {// 设置“与系统默认SeekBar对应的TextView”的值
            lastValue = progress;
            if (mViewType == 3) {
                DataManager.setBrightness(progress);
            } else {
                DataManager.setVolume(mViewType, progress);
            }
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onBackPressed() {
        if (lastValue != initValue) {
            if (mViewType == 3) {
                DataManager.sendLocalSocketIntent(DataManager.ACTION_BRIGHTNESS_CHANGED, lastValue);
            } else {
                DataManager.sendLocalSocketIntent(DataManager.ACTION_BRIGHTNESS_CHANGED, lastValue);
            }

            Intent intent = new Intent();
            intent.setAction(Constant.INENT_RESULT_ACTION);
            intent.putExtra(Constant.INENT_VIEW_TYPE, mViewType);
            intent.putExtra(Constant.INENT_RESULT_VALUE, lastValue);
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}