#[cfg(not(feature = "library"))]
// use cosmwasm_std::entry_point;
use cosmwasm_std::{Deps, DepsMut, Env, MessageInfo, Response, StdResult};
// use cw2::set_contract_version;

use crate::error::ContractError;
use crate::msg::{CountResponse, ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::{State, STATE};
// use crate::store;

// version info for migration info
// const CONTRACT_NAME: &str = "crates.io:addr-contract";
// const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

// #[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
    deps: DepsMut,
    _env: Env,
    info: MessageInfo,
    msg: InstantiateMsg,
) -> Result<Response, ContractError> {
    let state = State {
        count: msg.count,
    };

    STATE.save(deps.storage, &state)?;

    Ok(Response::new()
        .add_attribute("method", "instantiate")
        .add_attribute("owner", info.sender)
        .add_attribute("count", msg.count.to_string()))
}

// #[cfg_attr(not(feature = "library"), entry_point)]
// #[wasm_bindgen]
pub fn execute(
    deps: DepsMut,
    _env: Env,
    _info: MessageInfo,
    msg: ExecuteMsg,
) -> Result<Response, ContractError> {
    match msg {
        ExecuteMsg::Increment { by } => try_increment(deps, by ),
        ExecuteMsg::Reset { count } => try_reset(deps, count),
    }
}

pub fn try_increment(deps: DepsMut, by: Option<i32>) -> Result<Response, ContractError> {
    STATE.update(deps.storage, |mut state| -> Result<_,ContractError> {
        state.count += by.unwrap_or(1);
        Ok(state)
    })?;

    Ok(Response::new().add_attribute("method", "try_increment"))
}

pub fn try_reset(deps: DepsMut, count: i32) -> Result<Response, ContractError> {
    STATE.update(deps.storage, |mut state| -> Result<_, ContractError>{
        state.count = count;
        Ok(state)
    })?;
    Ok(Response::new().add_attribute("method", "reset"))
}

pub fn query(deps: Deps, msg: QueryMsg) -> StdResult<CountResponse> {
    match msg {
        QueryMsg::GetCount {} => query_count(deps),
    }
}

fn query_count(deps: Deps) -> StdResult<CountResponse> {
    let state = STATE.load(deps.storage)?;
    Ok(CountResponse { count: state.count })
}
