package com.xplora.xpsettings.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

import com.xplora.xpsettings.Adapter.BaseListAdapter;
import com.xplora.xpsettings.Data.DataManager;
import com.xplora.xpsettings.Model.IconProgressModel;
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;


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

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

    private void initData() {
        String[] ringTone = getResources().getStringArray(R.array.ringtone_title_array);
        String[] stringList = getResources().getStringArray(R.array.volume_title_array);
        for (int i = 0; i < stringList.length; i++) {
            IconProgressModel model = new IconProgressModel();
            model.title = stringList[i].trim();
            model.subTitle = ringTone[0];
            model.icon = ResUtils.getImageId("volume_", i);
            model.progress = DataManager.getVolume(i);
            model.isShowArrow = false;
            model.cellType = Constant.CellType.PROGRESS_TITLE;
            mDataList.add(model);
        }
    }

    private void initView() {
        mAdapter = new BaseListAdapter(this, 0, mDataList);
        ListView listView = findViewById(R.id.activity_listview);
        listView.setAdapter(mAdapter);

        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent = new Intent(this, ChangeProgressActivity.class);
            intent.putExtra(Constant.INENT_VIEW_TYPE, i);
            startActivityForResult(intent, 0);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null)
            return;
        if (data.getAction().equals(Constant.INENT_RESULT_ACTION)) {
            int viewType = data.getIntExtra(Constant.INENT_VIEW_TYPE, 0);
            int value = data.getIntExtra(Constant.INENT_RESULT_VALUE, 0);

            IconProgressModel model = mDataList.get(viewType);
            model.progress = value;
            mAdapter.setBaseObjects(mDataList);
            mAdapter.notifyDataSetInvalidated();
        }
    }
}