package com.xplora.xpsettings.Activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
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;

import java.util.Map;


public class AdjustSeekbarActivity extends BaseActivity implements SeekBar.OnSeekBarChangeListener {
    int mViewType = Constant.E_ADJUST.VOICE_SYSTEM.ordinal();
    int curValue = 0;
    int maxValue = 100;
    int minValue = 0;
    private ImageView mImageView = null;

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

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        initDatas();
        initView();
    }

    private void initDatas() {
        mViewType = getIntent().getIntExtra(Constant.INTENT_VIEW_TYPE, Constant.E_ADJUST.VOICE_SYSTEM.ordinal());
        if (mViewType == Constant.E_ADJUST.BRIGHTNESS.ordinal()) {
            curValue = DataManager.getBrightness();
        } else {
            Map map = DataManager.getVolume(mViewType);
            curValue = (int) map.get("cur");
            maxValue = (int) map.get("max");
            minValue = (int) map.get("min");
        }
        Log.d(TAG, "initData: curValue: " + curValue + " maxValue: " + maxValue + " minValue: " + minValue);
    }

    @SuppressLint({"DefaultLocale", "UseCompatLoadingForDrawables"})
    private void initView() {
        mImageView = findViewById(R.id.icon);
        TextView titleText = findViewById(R.id.title);
        SeekBar seekBar = findViewById(R.id.seekbar);

        if (titleText != null) {
            if (mViewType == Constant.E_ADJUST.BRIGHTNESS.ordinal()) {
                String[] stringList = ResUtils.getStringArray("display_title_array_", 4);
                titleText.setText(stringList[0]);
            } else {
                String[] stringList = ResUtils.getStringArray("volume_title_array_", 2);
                String[] soundModeList = ResUtils.getStringArray("soundmode_title_array_", 3);
                String title = soundModeList[mViewType];
                titleText.setText(title);
            }
        }
        if (seekBar != null) {
            seekBar.setMin(0);
            seekBar.setMax(maxValue);
            seekBar.setProgress(curValue);
            seekBar.setOnSeekBarChangeListener(this);

            if (mViewType == Constant.E_ADJUST.BRIGHTNESS.ordinal()) {
                seekBar.setProgressDrawable(getDrawable(R.drawable.seekbar_bg_brightness));
            } else {
                seekBar.setProgressDrawable(getDrawable(R.drawable.seekbar_bg_voice));
            }
        }

        changeImage();
    }

    private void changeImage() {
        if (mImageView == null)
            return;
        if (mViewType == Constant.E_ADJUST.BRIGHTNESS.ordinal()) {
            int imageId = ResUtils.getImageId("adjust_", mViewType);
            mImageView.setImageResource(imageId);
        } else {
            int index = curValue <= 0 ? 1000 : mViewType;
            int imageId = ResUtils.getImageId("adjust_", index);
            mImageView.setImageResource(imageId);
        }
    }

    /*
     * SeekBar滚动时的回调函数
     */
    @SuppressLint("SetTextI18n")
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (progress <= minValue) {
            progress = minValue;
            seekBar.setProgress(progress);
        }
        Log.d(TAG, "onProgressChanged: mViewType:" + mViewType +  " progress: " + progress);

        curValue = progress;
        if (mViewType == Constant.E_ADJUST.BRIGHTNESS.ordinal()) {
            DataManager.setBrightness(progress);
        } else {
            DataManager.setVolume(mViewType, progress);
        }

        changeImage();

        int value = mViewType == Constant.E_ADJUST.BRIGHTNESS.ordinal() ? curValue : (curValue * 100 / maxValue);
        Intent intent = new Intent();
        intent.putExtra(Constant.INTENT_VIEW_TYPE, mViewType);
        intent.putExtra(Constant.INTENT_RESULT_VALUE, value);
        setResult(RESULT_OK, intent);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}