package com.xplora.xpsettings.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;

import androidx.annotation.NonNull;

import com.xplora.xpsettings.Adapter.BaseListAdapter;
import com.xplora.xpsettings.Data.DataManager;
import com.xplora.xpsettings.Model.BaseModel;
import com.xplora.xpsettings.Model.IconProgressModel;
import com.xplora.xpsettings.R;
import com.xplora.xpsettings.Utils.Constant;
import com.xplora.xpsettings.Utils.ResUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class VolumeActivity extends BaseActionActivity {
    private final List<BaseModel> mDataList = new ArrayList<>();

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

    @Override
    protected void onResume() {
        super.onResume();

        mDataList.clear();
        initData();

        mAdapter.setBaseObjects(mDataList);
        mAdapter.notifyDataSetInvalidated();
    }

    private void initData() {
        String[] stringList = ResUtils.getStringArray("volume_title_array_", 2);
        for (int i = 0; i < stringList.length; i++) {
            if (i == 0 || i == 1) {
                IconProgressModel model = new IconProgressModel();
                model.title = stringList[i].trim();
                model.isShowArrow = false;
                model.subTitle = "";
                model.icon = ResUtils.getImageId("volume_", i);
                model.isShowArrow = false;
                model.cellType = Constant.CellType.PROGRESS_TITLE;
                Map map = DataManager.getVolume(i);
                int max = (int) map.get("max");
                int min = (int) map.get("min");
                int cur = (int) map.get("cur");

                //min要从0开始，call 最小值为1，
                model.progress = cur * 100 / max;
                Log.d(TAG, "initData: title: " + model.title + " progress: " + model.progress + " cur: " + cur + " max: " + max + " min: " + min);
                mDataList.add(model);
            } else {
                int isVibrate = DataManager.getIsVibrate();
                BaseModel model = new BaseModel();
                model.title = stringList[i].trim();
                model.isShowArrow = false;
                model.cellType = Constant.CellType.SWITCH;
                model.isOn = isVibrate >= 1;
                mDataList.add(model);
            }
        }
    }

    private void initView() {
        mAdapter = new BaseListAdapter(this, 0, mDataList, mHandler);
        ListView listView = findViewById(R.id.activity_listview);
        listView.setAdapter(mAdapter);
        listView.setDivider(new ColorDrawable(Color.TRANSPARENT));
        listView.setDividerHeight(0);

        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            if (i <= 1) {
                Intent intent = new Intent(this, AdjustSeekbarActivity.class);
                intent.putExtra(Constant.INTENT_VIEW_TYPE, i);
                startActivityForResult(intent, 112);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "onActivityResult: data:" + data);
        if (requestCode == 112 && resultCode == RESULT_OK) {
//            int viewType = data.getIntExtra(Constant.INTENT_VIEW_TYPE, 0);
//            int value = data.getIntExtra(Constant.INTENT_RESULT_VALUE, 0);
//
//            IconProgressModel model = (IconProgressModel)mDataList.get(viewType);
//            model.progress = value;
//            mAdapter.setBaseObjects(mDataList);
//            mAdapter.notifyDataSetInvalidated();
        }
    }

    Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == Constant.MSG_SWITCH_CHANGE) {
                int index = msg.arg1;
                int isOn = msg.arg2;
                BaseModel model = mDataList.get(index);
                model.isOn = isOn >= 1;
                DataManager.setIsVibrate(isOn);
            }
        }
    };
}