package com.xplora.xpsettings.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

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.ResUtils;

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


public class DisplayActivity 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() {
        int appNameType = DataManager.getAppNameType();
        int brightness = DataManager.getBrightness();

        String[] stringList = getResources().getStringArray(R.array.display_title_array);
        for (int i = 0; i < stringList.length; i++) {
            IconProgressModel model = new IconProgressModel();
            model.title = stringList[i].trim();
            if (i == 0) {
                model.icon = R.drawable.ic_brightness;
                model.progress = brightness;
                model.progressColor = R.color.xp_yellow;
                model.cellType = Constant.CellType.PROGRESS_TITLE;
            } else {
                model.cellType = Constant.CellType.SWITCH;
                model.isOn = i == appNameType;
            }
            mDataList.add(model);
        }

        IconProgressModel model = new IconProgressModel();
        model.cellType = Constant.CellType.MESSAGE;
        model.subTitle = getResources().getString(R.string.app_name_status_hint);
        mDataList.add(model);
    }

    private void initView() {
        ListView listView = findViewById(R.id.activity_listview);
        mAdapter = new BaseListAdapter(this, R.layout.item_xpstyle, mDataList, mHandler);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent;
            switch (i) {
                case 0:
                    intent = new Intent(this, ChangeProgressActivity.class);
                    intent.putExtra(Constant.INENT_VIEW_TYPE, 3);
                    startActivityForResult(intent, 0);
                    break;
                default:
                    break;
            }
        });
    }

    @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 value = data.getIntExtra(Constant.INENT_RESULT_VALUE, 0);

            IconProgressModel model = mDataList.get(0);
            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_SWITCHC_CHANGE) {
                int index = msg.arg1;
                int isOn = msg.arg2;
                IconProgressModel model = mDataList.get(index);
                model.isOn = isOn >= 1;
                DataManager.setAppNameType(isOn);
            }
        }
    };
}