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.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 AdjustGridActivity extends BaseActionActivity {
    int mViewType = 0;
    int minValue = 0;
    int maxValue = 100;
    int[] mBrightnessLevels = {20, 40, 60, 80, 100};
    int[] mVoiceLevels = {0, 30, 60, 100};
    int[] mCurLevels;
    int mCurLevel = 0;

    private ImageView mIcon = null;
    private ImageView mSubButton = null;
    private ImageView mAddButton = null;
    private TextView mProgressText = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adjust_grid);
        initDatas();
        initView();
        initTitle();
        changeImage();
        changeProgress();
    }

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

    private void initDatas() {
        int curValue = 0;
        mViewType = getIntent().getIntExtra(Constant.INTENT_VIEW_TYPE, 0);
        if (mViewType == 3) {
            curValue = DataManager.getBrightness();
            minValue = 0;
            maxValue = 100;
            mCurLevels = mBrightnessLevels;
        } else {
            Map map = DataManager.getVolume(0);
            curValue = (int)map.get("cur");
            maxValue = (int)map.get("max");
            minValue = (int)map.get("min");
            mCurLevels = mVoiceLevels;
        }
        mCurLevel = initLevel(curValue);
        Log.d(TAG, "initData: curLevel: " + mCurLevel +  " curValue: " + curValue + " maxValue: " + maxValue + " minValue: " + minValue);
    }

    private int initLevel(int curValue) {
        int per = (int)((curValue / (float) maxValue) * 100);
        for (int i = 0; i < mCurLevels.length; i++) {
            if (per <= mCurLevels[i]) {
                mCurLevel = i;
                return mCurLevel;
            }
        }
        return mCurLevels.length - 1;
    }

    private int exchangeSystemValue() {
        int value = mCurLevels[mCurLevel];
        return (int)((value / 100.0) * maxValue);
    }

    @SuppressLint("DefaultLocale")
    private void initView() {
        mIcon = findViewById(R.id.icon);
        mProgressText = findViewById(R.id.progressText);
        mSubButton = findViewById(R.id.subButton);
        mAddButton = findViewById(R.id.addButton);
        mSubButton.setOnClickListener( v -> clickSubButton());
        mAddButton.setOnClickListener( v -> clickAddButton());
    }

    private void initTitle() {
        TextView titleText = findViewById(R.id.title);
        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);
            }
        }
    }

    private void changeImage() {
        if (mIcon == null)
            return;
        if (mViewType == 3) {
            int imageId = ResUtils.getImageId("adjust_grid_bright_", mCurLevels[mCurLevel]);
            mIcon.setImageResource(imageId);
        } else {
            int imageId = ResUtils.getImageId("adjust_grid_voice_", mCurLevels[mCurLevel]);
            mIcon.setImageResource(imageId);
        }
    }

    @SuppressLint("SetTextI18n")
    private void changeProgress() {
        mProgressText.setText(mCurLevels[mCurLevel] + "%");
        mProgressText.setTextColor(mViewType == 3 ? getColor(R.color.sk_yellow) : getColor(R.color.sk_green));
        mSubButton.setEnabled(mCurLevel > 0);
        mAddButton.setEnabled(mCurLevel < mCurLevels.length-1);
    }

    private void clickSubButton() {
        mCurLevel = Math.max(mCurLevel-1, 0);
        changeImage();
        changeProgress();
        syncSystem();
    }

    private void clickAddButton() {
        mCurLevel = Math.min(mCurLevel+1, mCurLevels.length-1);
        changeImage();
        changeProgress();
        syncSystem();
    }

    public void syncSystem() {
        int value = exchangeSystemValue();
        if (mViewType == 3) {
            DataManager.setBrightness(value);
        } else {
            DataManager.setAllVolume(value);
        }
    }


//    /*
//     * 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);
//
//        mCurLevel = progress;
//        if (mViewType == 3) {
//            DataManager.setBrightness(progress);
//        } else {
//            DataManager.setVolume(mViewType, progress);
//        }
//
//        changeImage();
//
//        int value = mViewType == 3 ? mCurLevel : (mCurLevel * 100 / maxValue);
//        Intent intent = new Intent();
//        intent.putExtra(Constant.INTENT_VIEW_TYPE, mViewType);
//        intent.putExtra(Constant.INTENT_RESULT_VALUE, value);
//        setResult(RESULT_OK, intent);
//    }
}