package com.xplora.xpsettings.Activity;

import android.content.Intent;
import android.media.AudioManager;
import android.media.Ringtone;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

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

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


public class SoundModeActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_title);
        initView();
    }

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

        mDataList.clear();
        initData();

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

    private void initData() {
        Ringtone ringTone = DataManager.getDefaultRingTone();
        String[] stringList = ResUtils.getStringArray("soundmode_title_array_", 3);
        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("adjust_", i);
                model.progressColor = getColor(R.color.xp_green);
                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 if (i == 2){
                int isVibrate = DataManager.getIsVibrate();
                BaseModel model = new BaseModel();
                model.title = stringList[i].trim();
                model.isOn = isVibrate >= 1;
                model.cellType = Constant.CellType.SWITCH_RIGHT;
                mDataList.add(model);
            } else if (i == 3){
                BaseModel model = new BaseModel();
                model.title = stringList[i].trim();
                model.cellType = Constant.CellType.TITLE;
                model.subTitle = ringTone != null ? ringTone.getTitle(this) : "";
                mDataList.add(model);
            }
        }
    }

    private void initView() {
        mAdapter = new BaseListAdapter(this, R.layout.item_xpstyle, mDataList, mHandler);
        ListView listView = findViewById(R.id.activity_listview);
        listView.setDivider(null);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                clickItem(position);
            }
        });
    }

    private void refreshView(int position) {
        int ringerMode = viewMode2RingerMode(position);
        DataManager.setRingerMode(ringerMode);

        mDataList.clear();
        initData();

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

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void clickItem(int position) {
        switch (position) {
            case 0:
                Intent intent0 = new Intent(this, AdjustSeekbarActivity.class);
                intent0.putExtra(Constant.INTENT_VIEW_TYPE, Constant.E_ADJUST.VOICE_SYSTEM.ordinal());
                startActivityForResult(intent0, 110);
                break;
            case 1:
                Intent intent1 = new Intent(this, AdjustSeekbarActivity.class);
                intent1.putExtra(Constant.INTENT_VIEW_TYPE, Constant.E_ADJUST.VOICE_MUSIC.ordinal());
                startActivityForResult(intent1, 110);
                break;
            case 3:
                Intent intent2 = new Intent(SoundModeActivity.this, RingToneActivity.class);
                startActivityForResult(intent2, 112);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 112 && resultCode == RESULT_OK) {
            Ringtone ringTone = DataManager.getDefaultRingTone();
            BaseModel model = mDataList.get(2);
            model.subTitle = ringTone.getTitle(this);
            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);
            }
        }
    };

    public int ringerMode2ViewMode(int ringerMode) {
        if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
            return 2;
        } else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
            return 1;
        } else {
            return 0;
        }
    }

    public int viewMode2RingerMode(int viewMode) {
        if (viewMode == 2) {
            return AudioManager.RINGER_MODE_SILENT;
        } else if (viewMode == 1) {
            return AudioManager.RINGER_MODE_VIBRATE;
        } else {
            return AudioManager.RINGER_MODE_NORMAL;
        }
    }
}