package com.xplora.xpsettings.Activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
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 ChangeProgressActivity extends BaseActivity implements SeekBar.OnSeekBarChangeListener {
    int mViewType = 0;

    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_change_progress);
        initDatas();
        initView();
    }

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

    private void initDatas() {
        mViewType = getIntent().getIntExtra(Constant.INTENT_VIEW_TYPE, 0);
        if (mViewType == 3) {
            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")
    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 == 3) {
                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 = stringList[mViewType] + " " + soundModeList[0];
                titleText.setText(title);
            }
        }
        if (seekBar != null) {
            seekBar.setMin(0);
            seekBar.setMax(maxValue);
            seekBar.setProgress(curValue);
            seekBar.setOnSeekBarChangeListener(this);
        }

        changeImage();
    }

    private void changeImage() {
        if (mImageView == null)
            return;
        if (mViewType == 3) {
            int imageId = ResUtils.getImageId("ic_seekbar_", mViewType);
            mImageView.setImageResource(imageId);
        } else {
            int index = curValue <= 0 ? 0 : 2;
            int imageId = ResUtils.getImageId("ic_seekbar_", 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 == 3) {
            DataManager.setBrightness(progress);
        } else {
            DataManager.setVolume(mViewType, progress);
        }

        changeImage();

        int value = mViewType == 3 ? 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) {

    }
}